鉴于这一模式:
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值
发布于 2020-12-27 15:44:26
试试这个:
needs_attention = Q(comment__needs_attention=True)
Post.objects.annotate(last_comment_date=Max('comment__created_at'), filter=needs_attention)https://stackoverflow.com/questions/32555097
复制相似问题