gpt4 book ai didi

mysql - Django 不同 : COUNT vs len

转载 作者:行者123 更新时间:2023-11-29 23:30:58 25 4
gpt4 key购买 nike

我有一个带有一些字段的模型 M,其中三个已定义顺序。我们称它们为a、b、c。所以我有 ordering = ["a", "b", "c"]。

我的问题是:

n = M.objects.values_list ('a', flat=True).distinct()
n.count() --> 217
len(n) --> 26580

这怎么可能?

我使用 Python 2.7、Django 1.6 和 MySQL。

谢谢!

最佳答案

documentation on distinct()解决这一点:

If you use a values() query to restrict the columns selected, the columns used in any order_by() (or default model ordering) will still be involved and may affect uniqueness of the results.

如果您用自己的模型顺序覆盖默认模型顺序,它应该可以工作:

n = M.objects.order_by('a').values_list('a', flat=True).distinct()

(尽管如果 a 是相关字段,文档中还有更多警告。)

关于mysql - Django 不同 : COUNT vs len,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26577352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com