I would basically like to do the same as this question, but grouping by combinations of two values, rather than just one:
SELECT player_type, team, COUNT(*) FROM players GROUP BY player_type, team;
Does anyone know whether, and how, this is possible in Django? I'm using 1.2.
When specifying the field to be aggregated in an aggregate function, Django will allow you to use the same double underscore notation that is used when referring to related fields in filters. Django will then handle any table joins that are required to retrieve and aggregate the related value.
Appending the annotate() clause onto a QuerySet lets you add an attribute to each item in the QuerySet, like if you wanted to count the amount of articles in each category. However, sometimes you only want to count objects that match a certain condition, for example only counting articles that are published.
Player.objects.values('player_type', 'team').order_by().annotate(Count('player_type'), Count('team'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With