[ACCEPTED]-Django: Filtering on the related object, removing duplicates from the result-duplicates

Accepted answer
Score: 19

Adding .distinct() will give you only distinct results.


Score: 8

See the QuerySet API Docs for the "distinct()" function:

Returns 11 a new QuerySet that uses SELECT DISTINCT 10 in its SQL query. This eliminates duplicate 9 rows from the query results.

By default, a 8 QuerySet will not eliminate duplicate 7 rows. In practice, this is rarely a problem, because simple 6 queries such as Blog.objects.all() don't 5 introduce the possibility of duplicate 4 result rows. However, if your query spans 3 multiple tables, it's possible to get 2 duplicate results when a QuerySet is evaluated. That's 1 when you'd use distinct().

More Related questions