我有以下代碼: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_...
中發佈回溯哦,太棒了......這對我有用:y = Menu.objects.filter(level = 1).values_list('name',flat = True).order_by('name') 。不同() – Abhishek