[Fixed]-Django comment framework: distinct() does not work?

21👍

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

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

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

Leave a comment