Dav*_*542 20 python mysql django
我该如何进行以下查询:
OrderNotes.objects.filter(item=item).distinct('shared_note')
Run Code Online (Sandbox Code Playgroud)
基本上,我需要获得所有OrderNotes项目,区别于shared_note.当我尝试这样做时,我得到:
raise NotImplementedError('DISTINCT ON fields is not supported by this database backend')
NotImplementedError: DISTINCT ON fields is not supported by this database backend
Run Code Online (Sandbox Code Playgroud)
我使用的是mysql,无法在这里更改数据库.django的解决方法是什么?
cat*_*ine 68
OrderNotes.objects.filter(item=item).values_list('shared_note', flat=True).distinct()
Run Code Online (Sandbox Code Playgroud)
这是我想出的最好的:
>>> items, item_ids = [], []
>>> for item in OrderNotes.objects.filter(shared_note=219):
... if item.shared_note not in item_ids:
... items.append(item)
... item_ids.append(item.shared_note)
Run Code Online (Sandbox Code Playgroud)