Reputation: 110093
I have the following query:
titles = Title.objects.all()
I need to get DISTINCT ('title')
for this query under certain circumstances. Django does not allow distinct('title')
while using a MySQL backend.
Essentially, what I want to do is something like:
titles = titles.extra('GROUP BY title')
How would I do this correctly?
Upvotes: 1
Views: 854
Reputation: 4656
Have you tried using the .distinct()
method?
titles = Title.objects.values('title','other_columns_you_care_about_and_not_distinct').distinct()
If you do a .distinct()
on a .all()
you will return as many rows as you started with, provided of course that your id column is unique. So, you only pull back the columns you need in the .values()
.
Upvotes: 1
Reputation: 99620
You need to do [annotate][1]
to group by
titles = titles.annotate(Count("title")).order_by()
Upvotes: 0