我有以下型号:
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')