I would like to compute the maximum of \"a_priority\" for each group of (b, c) pairs.
a_priority is an annotation based on a case/when mapping strings to priority va
Have you tried putting Case
expression directly into Max
? It is possible since Django 1.8.
from django.db.models import Max, Case, When, IntegerField
qs = MyObject.objects.all()
a_priority=Case(
When(a='A', then=1),
When(a='S', then=2),
When(a='Q', then=3),
output_field=IntegerField()
)
qs = qs.values("b", "c").annotate(max_a_priority=Max(a_priority))