I am performing the following query with a few annotations:
(AwardIssueProcess.objects.filter(grant__client=client, completed=completed_status)
.annotate(total_awarded=Sum('award__awardissuedactivity__units_awarded'),
num_accepted=Count(Case(When(award__accepted=True, then=1))),
num_rejected=Count(Case(When(award__accepted=False, then=1))),
num_unaccepted=Count(Case(When(award__accepted=None, then=1)))))
However, the num_unaccepted
yields incorrect values. Firstly, if I have awards the number is sometimes double what I expect. But if I remove
total_awarded=Sum('award__awardissuedactivity__units_awarded')
from the annotation, then the doubling problem goes away.
Secondly, the num_unaccepted
has a value of 1 if there are no awards. But when there are awards, the value is correct (but not is all cases due to the doubling problem I mentioned previously). In this second issue, I suspect that this may be because it is evaluating the award to be None
, but what I really want is for it to check if the accepted
field is None
. And then if the award doesn't exists, just don't count it. The accepted
field is a NullBooleanField
.
How should I be writing this differently to solve these two problems with num_unaccepted
?
EDIT
I removed total_awarded=Sum('award__awardissuedactivity__units_awarded')
from the annotation and created a separate function to get the total_awarded
amount that I need. This addresses my first problem, but the second issue still remains.