6👍
I haven’t verified that this is the cause, but Comment
model has a default ordering which influences 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
It’s a documented feature.
Now, how could it be that two comments have exactly the same timestamp? I suppose you’re using MySQL which doesn’t support anything less than a second.
And if you want to get rid of the default ordering, just do:
Comment.objects.order_by().values('ip_address').distinct()
- Django – (OperationalError) FATAL: Ident authentication failed for user "username"
- How to profile django channels?
- Django-admin.py startproject opens notepad, instead of creating a project
- {% load static %} and {% load staticfiles %}: which is preferred?
- Django – How to filter by date with Django Rest Framework?
2👍
You can wrap your query in set;
distinct() does not go well with values() as per documentation
ip_sets = set(Comment.objects.order_by().values('ip_address'))
ip_list = list(set(Comment.objects.order_by().values('ip_address')))
- How can I get a textarea from model+ModelForm?
- Django equivalent for latest entry for each user
- Web2py in the future?
Source:stackexchange.com