在Django中选择distinct [英] Select distinct in Django
问题描述
我在这里做错什么?
[app.system_name for app in App.objects.all().distinct('system_name')]
给我:
[u'blog', u'files', u'calendar', u'tasks', u'statuses', u'wiki', u'wiki', u'blog
', u'files', u'blog', u'ideas', u'calendar', u'wiki', u'wiki', u'statuses', u'ta
sks', u'survey', u'blog']
正如你所料,我希望所有的独特值为field_name,但现在我刚刚获取所有的App实例。
As you might expect I want all the unique values of the field system_name, but now I just get all App instances back.
推荐答案
-
仅在Django 1.4+中支持指定
distinct
中的字段。如果你正在运行1.3,那只是忽略它。
Specifying fields in
distinct
is only supported in Django 1.4+. If you're running 1.3, it's just ignoring it.
如果你运行Django 1.4,你必须添加一个 order_by
子句包含并以 distinct
中的所有字段开始。
If you are running Django 1.4, you must add an order_by
clause that includes and starts with all the fields in distinct
.
即使这样,使用 distinct
指定字段仅支持PostgreSQL。如果你正在运行别的东西,比如MySQL,你就不幸了。
Even then, specifying fields with distinct
is only support on PostgreSQL. If you're running something else, such as MySQL, you're out of luck.
在文档。
这篇关于在Django中选择distinct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!