我有两个模型描述如下:
class Report(models.Model):
report_name = models.CharField(max_length=140)
report_description = models.TextField(null=True, blank=True)
last_updated = models.DateTimeField(null=True, blank=True)
metric = models.ManyToManyField(Metric, through='MetricAssociation')
class Status(models.Model):
report = models.ForeignKey(Report, on_delete=models.CASCADE)
status = models.CharField(max_length=140)
provided_by = models.ForeignKey(TeamMember, on_delete=models.CASCADE)
date = models.DateTimeField()
我已经创建了一个包含所有报表和属性的索引页。我只想传递每个报表的最新状态,以及要在如下视图中使用的报表列表:
<td>{{ report.report_name }}</td>
<td>{{ report.last_updated }}</td>
<td>{{ report.latest_status }}</td>
由于报表没有最新的状态属性,但理想情况下,最新的状态是该报表的最新状态,我如何传递该属性
很简单:
Report
实例的“最新状态”是现在,由于这有点冗长,暴露了太多的实现,并且无法从模板访问,因此您只需将调用封装在getter方法或
property
中,即:相关问题 更多 >
编程相关推荐