鉴于此模型:
from django.db import models
class Post(models.Model):
text = models.CharField(max_length=255)
class Comment(models.Model):
text = models.CharField(max_length=255)
needs_attention = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
post = models.ForeignKey(Post)
我可以annotate
帖子列表:
Post.objects.annotate(last_comment_date=Max('comment__created_at'))
了解last_comment_date
,如何注释以设置last_comment_needs_attention
的值?
答案 0 :(得分:1)
试试这个:
needs_attention = Q(comment__needs_attention=True)
Post.objects.annotate(last_comment_date=Max('comment__created_at'), filter=needs_attention)