使用最新日期和相应的子记录Django注释查询行

时间:2016-12-14 17:01:03

标签: django django-models

我有以下型号:

class Post(Model):
    word = TextField()
    subscribers = ManyToManyField(User, related_name='subscribed',    through='Subscription')

class Subscription(Model):
    post = ForeignKey(Post)
    subscriber = ForeignKey(User)
    date_subscribed = DateTimeField(default=timezone.now)

    class Meta:
        ordering = ('-date_subscribed', )
        unique_together = (('post', 'subscriber'))

我想用最新的订阅日期和相应的订阅者注释所有帖子。 怎么做?

现在我有以下查询(感谢@AKS Django manytomany query weird behavior):

 posts = Post.objects.annotate(
            s_count=Count('subscribers'),
            s_date_max=Max('subscription__date_subscribed')
        ).order_by('-s_count', '-s_date_max')

0 个答案:

没有答案
相关问题