Django注释Count查询输出
我对这些查询的结果感到困惑:
>>> [f.count for f in Favourite.objects.annotate(count = Count('object_id'))]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
第二个查询是
>>> [f['count'] for f in Favourite.objects.values('object_id').annotate(count=Count('object_id'))]
[1, 5, 2, 1, 4, 2, 2, 3]
根据 django docs 第一个查询应该可以正常工作,并且 Favourite
对象具有 object_id
计数。
谁能解释为什么第二个查询有效,但第一个查询无效?
谢谢!
I have confusion in result of these queries:
>>> [f.count for f in Favourite.objects.annotate(count = Count('object_id'))]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
and second one is
>>> [f['count'] for f in Favourite.objects.values('object_id').annotate(count=Count('object_id'))]
[1, 5, 2, 1, 4, 2, 2, 3]
but according to django docs first query should work fine, and Favourite
object have count of object_id
.
Can anyone explain why the second query is working, but not the first?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第二个是对 object_id 进行
group by
。这是预期的行为。第一个是简单地计算数据库中每一行的 object_id 。The second one is doing a
group by
on object_id. This is the expected behaviour. The first one is simply counting object_id for each row in the database.