【发布时间】:2015-02-09 14:46:57
【问题描述】:
我有 2 个模型:
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-result 添加到字段中:
self.fields[field].queryset = sql_result
到这里为止,一切正常。
我的问题: 在选择框中显示了模型 PR_Components 的 str-Method 的结果 (=comp_nr),但我还想显示组件名称,例如“Component (001)”。
我怎么能这样做?它应该是一个适用于其他模型的解决方案,因为我的很多模型都有“历史”数据。
非常感谢
【问题讨论】: