Getting Distinct Rows Using Django Database Layer

Thu, Oct 1, 2009

Tech Tips

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


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

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.

Bookmark and Share

2 Responses to “Getting Distinct Rows Using Django Database Layer”

  1. Hipolito M. Wiseman Says:

    Thanks for this post. I am new at development and this got me straight.

  2. Filipe Says:

    Thanks! =D