Django Unique返回的记录多于计数 [英] Django distinct returns more records than count
问题描述
以下显示了不同的计数为2247
The following shows the distinct count is 2247
In [6]: VirtualMachineResources.objects.all().values('machine', 'cluster')
...: .distinct().count()
Out[6]: 2247
但是当我循环遍历它时,它返回的内容比应有的多:
But then when I loop through it, it returned way more than it should:
In [4]: a = []
...: for resource in VirtualMachineResources.objects.all().values('mach
...: ine', 'cluster').distinct():
...: if resource['cluster']:
...: a.append(resource['cluster'])
...:
...:
In [5]: len(a)
Out[5]: 96953
给定记录的簇字段为空,当我遍历查询集时,我可以看到同一台机器的很多重复而没有提示er值,但我只期望一次。
Given the records have cluster field being empty, when I loop through the queryset, I can see a lot of repeats of the same machine without cluster value, but I would expect only once.
for resource in VirtualMachineResources.objects.all().values('machine', 'cluster').distinct():
print(resource['machine'], resource['cluster'])
打印...
server1
server1
server1
这是一个postgres数据库。有任何想法吗?
在Google和这似乎有关联吗?
It's a postgres database. Any ideas? Having a few more digs on google and this seems related?
更新:
创建了Django问题此处
推荐答案
您已经定义了 VirtualMachineResources.Meta.ordering
会混淆ORM,因为使用 DISTINCT
时,按列排序必须出现在 SELECT
子句中。使用 .count()
时,顺序会被清除。
You have a defined VirtualMachineResources.Meta.ordering
that confuses the ORM as the ordered by columns must appear in the SELECT
clause when using DISTINCT
. The ordering happens to be cleared when using .count()
.
长存储短时,添加 .order_by()
将您的 Meta.ordering
放到您要遍历的查询集上,应该很好。没有很好的方法来生成 DISTINCT
来排除Django ORM上的排序字段,因为这将需要复杂的子查询下推,如#24218 。
Long storing short, add a .order_by()
to drop your Meta.ordering
on the queryset you are iterating over and you should be good to go. There's no good way to generate a DISTINCT
that excludes ordering fields on Django's ORM yet as that would require a complex subquery pushdown as detailed in #24218.
顺便说一句,请避免再次使用Django的票跟踪器层支持渠道,当您没有尽快收到您对问题的答复时。
By the way, please avoid Django's ticket tracker as a second tier support channel when you are not receiving a reply to your question as fast as you'd like.
这篇关于Django Unique返回的记录多于计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!