To get the equivalent of the SQL select distinct statement in Django, you can use a combination of the values() and distinct() method of the QuerySet api.
For example, you have a musicians table and you want to get list of instruments that they play (with no duplicates):
Musicians.objects.values('instrument').distinct()
The values(‘instrument’) method will generate a select statment of just the column instrument, while the distinct() method will apply the SELECT DISTINCT to the sql statement. The equivalent sql generated is: “select distinct instrument from musician”
One item to note (mentioned in the documentation), is when using the orderby() method to sort your data, the fields specified in the order by will also be included in the select statement. So you just have to be aware of it, especially if your results are not coming out as expected.
For example:
# Possible gotcha Musicians.objects.values('instrument').distinct().order_by('instrument_model_num')
The equivalent sql that is generated is “select distinct instrument, instrument_model_num from musician order by instrument_model_num”. This will not get you a list of just the instruments that the musicians play.
March 6th, 2010 at 12:34 pm
Thanks for this post. I am new at development and this got me straight.
July 28th, 2011 at 11:33 am
Thanks! =D