Reputation: 3621
Given this model:
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)
I can annotate
the list of posts with:
Post.objects.annotate(last_comment_date=Max('comment__created_at'))
to know the last_comment_date
, how to annotate to set a value for last_comment_needs_attention
?
Upvotes: 1
Views: 349
Reputation: 36
Try this:
needs_attention = Q(comment__needs_attention=True)
Post.objects.annotate(last_comment_date=Max('comment__created_at'), filter=needs_attention)
Upvotes: 1