django评论框架:distinct()不起作用? [英] django comment framework: distinct() does not work?
问题描述
在注释模型的任何字段上运行distinct()总是返回所有记录,
Comment.objects.values('user').distinct()
[{'用户':1},{'用户':0},{'用户':0},{'用户':0},{'用户':0}, {'用户':1},{'用户':1},{'用户':1},{'用户':1}]
Comment.objects.values('ip_address').distinct()
[{'ip_address':u'127.0.0.1'},{'ip_address':u'192.168.0.180'}, {'ip_address':u'192.168.0.180'},{'ip_address':u'192.168.0.180'}, {'ip_address':u'192.168.0. 180'},{'ip_address':u'192.168.0.180'}, {'ip_address':u'192.168.0.180'},{'ip_address':u'192.168.0.180'}, {'ip_address':u'192.168.0.180'}]
为什么会这样?有没有解决的办法?谢谢!
PS:在我的测试过程中,distinct()在自定义模型的不同类型的字段中运行得非常好.评论框架有什么特别之处吗?结论点 谢谢大家回答这个问题,再加上一些阅读,我得出以下结论:
- values()影响最终结果的SELECT部分中的查找字段 sql("<a href="https://docs.djangoproject.com/zh-CN/1.3/ref/models/querysets/#values" rel="noreferrer"> values()采用可选的位置参数* fields 指定SELECT应该限制的字段名称)
- order_by()也将其参数添加到SELECT部分.
-
在查找中使用distinct()将导致sql看起来像这样:
选择 DISTINCT [fields1,fields2,fields3]从...位置...
和字段的值共同决定记录是否唯一.这些字段可能来自查找中的values()或order_by()函数.
-
因此order_by()添加了一些不必要的效果当与distinct()结合使用时,order_by中指定的字段也要考虑到记录是否唯一
-
默认情况下,Django Comment具有一个隐藏的order_by参数,从而引发了整个问题.任何模型在返回qs时都具有隐藏的order_by可能导致相同的问题.
- 解决该问题的方法是通过在末尾添加一个空的order_by() 查找,这将删除默认的order_by.
Comment.objects.values('user').distinct().order_by()
Running distinct() on any field of the comment model always returns all the records,
Comment.objects.values('user').distinct()
[{'user': 1}, {'user': 0}, {'user': 0}, {'user': 0}, {'user': 0}, {'user': 1}, {'user': 1}, {'user': 1}, {'user': 1}]
Comment.objects.values('ip_address').distinct()
[{'ip_address': u'127.0.0.1'},{'ip_address': u'192.168.0.180'}, {'ip_address':u'192.168.0.180'}, {'ip_address': u'192.168.0.180'}, {'ip_address': u'192.168.0. 180'}, {'ip_address': u'192.168.0.180'}, {'ip_address': u'192.168.0.180'}, {'ip_address': u'192.168.0.180'}, {'ip_address': u'192.168.0.180'}]
Why is this happening? Is there a way around this? Thanks!
ps: distinct() does run very well in different types of fields of a custom model during my test. Something special about the Comments framework?
Bit of conclusion Thanks everybody answering this question, combined with some reading I get conclusion as following:
- values() influences the lookup fields in SELECT part of the final sql("values() takes optional positional arguments, *fields, which specify field names to which the SELECT should be limited")
- order_by() adds its parameter to the SELECT part as well.
using distinct() in a look up will result the sql to look like this:
SELECT DISTINCT [fields1, fields2, fields3] FROM ... WHERE...
and the values of the fields all together decides whether a record is unique. The fields may come from values() or order_by() functions in the lookup.
Django Comment has a hidden order_by parameter by default, thus creating the whole problem. Any model has a hidden order_by when returning the qs can cause the same problem.
- The way of solving it is by adding an empty order_by() at the end of the lookup, which removes the default order_by.
Comment.objects.values('user').distinct().order_by()
这篇关于django评论框架:distinct()不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!