Django comment framework: distinct () not working?

Running different () in any field of the comment model always returns all records,

Comment.objects.values ​​('user'). Different ()

[{'user': 1}, {'user': 0}, {'user': 0}, {'user': 0}, {'user': 0}, {'user': 1}, { 'user': 1}, {'user': 1}, {'user': 1}]

Comment.objects.values ​​('ip_address'). Different ()

[{'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 any way around this? Thanks!

ps: distinct () works fine in different types of user model fields during my test. Something special about the comment structure?

End Bit Thanks to everyone who answers this question, in combination with some reading, I get an opinion as follows:

+4
source share
2 answers
Comment.objects.values('user').distinct().order_by() 
+6
source

I have not confirmed that this is the reason, but the Comment model has a default order, which affects the distinct() method:

 In [1]: print Comment.objects.values('ip_address').distinct().query SELECT DISTINCT "django_comments"."ip_address", "django_comments"."submit_date" FROM "django_comments" ORDER BY "django_comments"."submit_date" ASC 

This is a documentary function .

Now, how can it be that two comments have exactly the same timestamp? I suppose you are using MySQL which does not support anything less than a second.

And if you want to get rid of the default order, just do:

 Comment.objects.order_by().values('ip_address').distinct() 
+3
source

Source: https://habr.com/ru/post/1393152/


All Articles