django comment framework: distinct() does not work? django comment framework: distinct() does not work? django django

django comment framework: distinct() does not work?


Comment.objects.values('user').distinct().order_by()


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().querySELECT 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()


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')))