2015-10-28 57 views
1

我有以下代碼:Django的過濾器不同,價值觀問題

>>> y = Menu.objects.filter(level=1).values('name').distinct() 
>>> y 
[{'name': u'Desserts'}, {'name': u'Wings'}, {'name': u'Biriyani'}, {'name': u'Starters'}, {'name': u'Desserts'}, {'name': u'Starters'}, {'name': u'Biriyani'}] 

它不給我一個不同的結果集。我怎樣才能實現不同的價值觀?

根據這兩個解決方案,我得到了錯誤:

Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "/Users/abhishek.yeruva/workspace/myreps/lib/python2.7/site-packages/django/db/models/query.py", line 138, in __repr__ 
    data = list(self[:REPR_OUTPUT_SIZE + 1]) 
    File "/Users/abhishek.yeruva/workspace/myreps/lib/python2.7/site-packages/django/db/models/query.py", line 162, in __iter__ 
    self._fetch_all() 
    File "/Users/abhishek.yeruva/workspace/myreps/lib/python2.7/site-packages/django/db/models/query.py", line 965, in _fetch_all 
    self._result_cache = list(self.iterator()) 
    File "/Users/abhishek.yeruva/workspace/myreps/lib/python2.7/site-packages/django/db/models/query.py", line 1217, in iterator 
    for row in compiler.results_iter(): 
    File "/Users/abhishek.yeruva/workspace/myreps/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 794, in results_iter 
    results = self.execute_sql(MULTI) 
    File "/Users/abhishek.yeruva/workspace/myreps/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 840, in execute_sql 
    cursor.execute(sql, params) 
    File "/Users/abhishek.yeruva/workspace/myreps/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute 
    return super(CursorDebugWrapper, self).execute(sql, params) 
    File "/Users/abhishek.yeruva/workspace/myreps/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/Users/abhishek.yeruva/workspace/myreps/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "/Users/abhishek.yeruva/workspace/myreps/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
ProgrammingError: SELECT DISTINCT ON expressions must match initial ORDER BY expressions 
LINE 1: SELECT DISTINCT ON ("myapp_menu"."name") "myapp_... 

回答

1

note here - 如果你正在使用order_by您訂購的領域將包括在DISTINCT。您在查詢中不顯示order_by,但如果您在Menu模型的Meta類中有一組,它仍然適用。

+0

中發佈回溯哦,太棒了......這對我有用:y = Menu.objects.filter(level = 1).values_list('name',flat = True).order_by('name') 。不同() – Abhishek

2

使用values_list而不是值,並設置平參數values_list:

y = set(Menu.objects.filter(level=1).values_list('name', flat=True)) 

傳遞你的價值列表設置功能,它創建一個具有獨特價值的集合,如Joey Wilhelm在評論中寫的。

+0

這仍然沒有給我不同的價值...我得到這個:[u'Desserts',u'Wings',u'Biriyani',u'Starters',u'Desserts',u'Starters',u 'Biriyani'] – Abhishek

+0

同樣的錯誤我提到喬伊與獨特('姓名') – Abhishek

+0

如果你想使用values_list,你會想要做的事情就像:'y = set(Menu.objects.filter(level = 1) .values_list('name',flat = True))'。但是,請注意,如果這是您所期望的,那麼這不會給您一個QuerySet。 –

2

除了Dima的解決方案,如果您使用的是PostgreSQL,您可以指定在哪個字段上執行不同查詢,如.distinct() documentation中所述。例如:

y = Menu.objects.filter(level=1).distinct('name').values('name') 
+0

這給了我一個編程錯誤 – Abhishek

+0

@ RogerFederer哪個錯誤? – vyscond

+0

我會在問題 – Abhishek