25

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.

Community
  • 1
  • 1
AP257
  • 81,509
  • 84
  • 194
  • 260

1 Answers1

27
Player.objects.values('player_type', 'team').order_by().annotate(Count('player_type'), Count('team'))
Amarghosh
  • 57,064
  • 11
  • 89
  • 120
  • 20
    This answer would actually return a query with two calls to COUNT, both player_type and team. As Django does not support the asterisk parameter for the count, COUNT(*) can be achieved by using a non-null field. The PK is a good candidate for this. thus using Count('pk') would be the right answer... – Vajk Hermecz Jan 25 '13 at 09:30