【发布时间】:2016-12-14 08:20:55
【问题描述】:
我有以下型号:
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'))
我要做的是选择所有帖子,按订阅者数量排序,如果订阅者数量相等,则按最后date_subscribed排序。
我的输入数据:
post1 = Post(text="post1")
post2 = Post(text="post2")
post3 = Post(text="post3")
post4 = Post(text="post4")
user1 = User(username="user1")
user2 = User(username="user2")
user3 = User(username="user3")
user4 = User(username="user4")
Subscription.objects.create(post=post1, user=user1)
Subscription.objects.create(post=post2, user=user1)
Subscription.objects.create(post=post3, user=user1)
Subscription.objects.create(post=post3, user=user2)
Subscription.objects.create(post=post3, user=user3)
Subscription.objects.create(post=post3, user=user4)
Subscription.objects.create(post=post4, user=user1)
Subscription.objects.create(post=post4, user=user2)
Subscription.objects.create(post=post4, user=user3)
此查询按预期工作,但未按date_subscribed 排序:
Post.objects.annotate(s_count=Count('subscribers')).order_by('-s_count')
当我写作时:
Post.objects.annotate(s_count=Count('subscribers')).order_by('-s_count', '-subscription__date_subscribed')
我得到了奇怪的结果,我并不真正理解这种行为。对于上述数据,它会输出带有s_count=1 的所有帖子。
为什么s_count 是 1?还有,最后date_subscribed如何正确下单?
更新:
还有一个问题。为什么Post.objects.annotate(s_count=Count('subscribers')).order_by('-s_count', '-subscription__date_subscribed').count() 在订阅中给出 4 而不是行数?
【问题讨论】: