Reputation: 20856
I have a django model query that needs to fetch me the distinct rows from a particular table, but for some reason its not fetching it.
Let me know your inputs
Query
Volunteercontacts.objects.order_by('name')- gives me the expected answer ie;ordering by name
Volunteercontacts.objects.order_by('name').distinct('name')- does not eliminate the duplicate
Upvotes: 1
Views: 1455
Reputation: 46
Django 1.4 provides the expected behaviour in the original question (assuming you are using Posgtres):
https://docs.djangoproject.com/en/1.4/ref/models/querysets/#distinct
Upvotes: 0
Reputation: 51645
You should differentiate between distinct Volunteercontacts
models and distinct name
column values.
For distinct Volunteercontacts
models you can use distinct(), but in this case has no effect:
Volunteercontacts.objects.order_by('name').distinct()
For distinct columns values you can use a dictionary or array of value list:
Volunteercontacts.objects.values_list('name',
flat=True).order_by('name').distinct()
Also, remember that the ability to specify field names in distinct method is only available in PostgreSQL.
Upvotes: 1