0
這個想法很簡單,我必須在我的func中查詢幾個模型。爲了保持它在我的模板中的清潔,我決定抽象一個包含我的數據的新模型。我正在通過lambda創建模型。如何查詢嵌套的外鍵
這裏是模型。
class Contact(models.Model):
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
lastname = models.CharField(max_length=50, null=True, blank=True)
firstname = models.CharField(max_length=50, null=True, blank=True)
email = models.EmailField(null=True, blank=True)
class CRecord(models.Model):
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
place = models.ForeignKey(Place, related_name="record")
contact = models.ForeignKey(Contact, blank=True, null=True, related_name="records")
class Place(models.Model):
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
deleted_date = models.DateTimeField(blank=True, null=True)
name = models.CharField(max_length=100, db_index=True)
lat = models.FloatField(db_index=True)
lon = models.FloatField(db_index=True)
這裏是我的lambda來製作新的對象:
tr_contact = lambda x: dict(first_name=x.firstname,
last_name=x.lastname,
owned_place=?)
基本上我想要的那些獲得owned_place是包含在CRecord的地方。我正在努力的部分是,每個聯繫人都有幾條記錄,每條記錄中有幾個地方。 owned_place字段應該等於每個記錄中包含的每個地方。
我已經嘗試了一些,甚至不知道是否真的有可能在一個請求..
你想做什麼?你是在創建一個新的對象,還是在過濾一些對象?您嘗試創建/過濾哪種類型的對象?你的問題不清楚這些細節。 – AKS
我正在嘗試過濾。我談論創建的部分是嚴格的關於模板,你是對的,實際上並不清楚..我試圖過濾。我篩選了所有聯繫人並希望檢索每條記錄中的所有地點(對於單個聯繫人可能有多個記錄) – bottus
請在您嘗試執行此篩選的地方添加相關代碼(視圖或任何功能)。 – AKS