Django高级注释

时间:2015-09-13 22:19:23

标签: django python-2.7 django-models

鉴于此模型:

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的值?

1 个答案:

答案 0 :(得分:1)

试试这个:

needs_attention = Q(comment__needs_attention=True)
Post.objects.annotate(last_comment_date=Max('comment__created_at'), filter=needs_attention)