Django ORM group by

Filtering in Django over foreign keys or many to many relations’s values has, as a buy product, the possibility of creating duplicates of the object you are actually filtering from. A simple way to avoid this is to group by on the primary key of the model. Django, however, does not have group by in the ORM. There is however a way to make Django add it. By slightly abusing annotation it is possible to add just such a claus.

from django.db.models import Count obj1.objects.filter(obj2__value=1).annotate(Count(‘pk’))

This will add the group by on the obj1 primary key and avoid duplication on the result set.