Django equivalent of COUNT with GROUP BY
original source: https://stackoverflow.com/questions/842031/django-equivalent-of-count-with-group-by
If you are using Django 1.1 beta (trunk):
Player.objects.values('player_type').order_by().annotate(Count('player_type'))
values('player_type')
– for inclusion onlyplayer_type
field intoGROUP BY
clause.order_by()
– for exclusion possible default ordering that can cause not needed fields inclusion inSELECT
andGROUP BY
.