我有这样一个模型表
class Comment(models.Model):
STATUS = (
(1, 'normal'),
(0, 'deleted'),
)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
body = models.TextField() # set the widget
status = models.IntegerField(choices=STATUS)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('-date_created',)
def __str__(self):
return self.body
我想在模板中选择最新更新的评论:
{% for comment in article.comment_set.all() %}
{% if comment.date_updated is the latest %}
{{ comment.date_updated | timesince}}
{% endfor %}
有可能在模板中实现这一点吗?
有可能,最新的评论将是
article.comment_set.latest('-date_updated')