0
SQL查询的结果我有2种型号:的Django与python3,在选择框
PR_Components (models.Model):
companyID = models.ForeignKey(PO_Company, blank=True, null=True)
comp_nr = models.CharField (max_length=5, blank=True, null=True)
def __str__(self):
return self.comp_nr
PR_ComponentsData (models.Model):
compID = models.ForeignKey (PR_Components, blank=False, null=True)
valid = models.DateField (max_length=10, blank=False, null=True)
comp_image = models.ImageField (upload_to="/images", blank=True, null=True)
comp_text = models.CharField (max_length=30, blank=False, null=True)
....
我想在一个选择框,现在中显示的组件数量(PR_Components.comp_nr)和当前有效的名称(PR_Componentsdata.comp_text )。
我添加了一个管理器来建模PR_Components执行SQL查询。
SELECT a.*, b1.* FROM pool_pr_components a
JOIN pool_pr_componentsdata b1 ON (a.id = b1.compID_id)
LEFT OUTER JOIN pool_pr_componentsdata b2
ON (a.id = b2.compID__id AND (b1.valid < b2.valid OR b1.valid = b2.valid
AND b1.id < b2.id)) WHERE b2.id IS NULL
后来我写的形式动态地和SQL-结果添加到字段:
self.fields[field].queryset = sql_result
直到这里,一切工作正常。
我的问题: 在模型PR_Components的海峡 - 方法的结果显示(= comp_nr)的选择框,但我想也显示组件名称,如“组件(001)”。
我该怎么做?它应该是一个适用于其他模型的解决方案,因为我的很多模型都有“历史”数据。
非常感谢