I am building a complicated queryset over a complicated datamodel.
The queryset must return a selection of model objects A
with an annotation ann
.
For computing ann
, I need an auxiliary annotation aux
, but no aux
is allowed to exist in the final results of the queryset.
qs = A.objects.filter(...) # complicated
qs = qs.annotate(aux=...) # complicated
qs = qs.annotate(ann=Case(
When(condition=Q(aux=0),
then Value('some')),
When(condition=Q(aux_gt=0),
then Value('other'))))
How to return qs
with ann
but without aux
?
(Alternatively: If aux
is a count and ann
discriminates zero and non-zero aux
, is there
a better way to approach the whole problem?)