django group by

Solutions on MaxInterview for django group by by the best coders in the world

showing results for - "django group by"
Giuseppe
20 May 2016
1# If you mean to do aggregation you can use the aggregation features of the ORM:
2from django.db.models import Count
3Members.objects.values('designation').annotate(dcount=Count('designation'))
4
5# This results in a query similar to:
6SELECT designation, COUNT(designation) AS dcount
7FROM members GROUP BY designation
8
9#and the output would be of the form
10[{'designation': 'Salesman', 'dcount': 2}, 
11 {'designation': 'Manager', 'dcount': 2}]