From the examples at the link (the cheat sheet), what example do you see in there that most closely looks like what you’re trying to do here?
This one.
>>> from django.db.models import Q
>>> above_5 = Count('book', filter=Q(book__rating__gt=5))
>>> below_5 = Count('book', filter=Q(book__rating__lte=5))
>>> pubs = Publisher.objects.annotate(below_5=below_5).annotate(above_5=above_5)
>>> pubs[0].above_5
But this method returns:
section_1 = Sum('answer__choice_value', filter=Q(question_id__in=[133]))
section_2 = Sum('answer__choice_value', filter=Q(question_id__in=[134,135,136,137,138,139]))
{'id': 1704, 'question_id': 133, 'answer_id': 567, 'value': '', 'notes': '', 'response_id': 113, 'section_1': 2, 'section_2': None}
{'id': 1765, 'question_id': 134, 'answer_id': 572, 'value': '', 'notes': '', 'response_id': 113, 'section_1': None, 'section_2': 1}
{'id': 1766, 'question_id': 135, 'answer_id': 576, 'value': '', 'notes': '', 'response_id': 113, 'section_1': None, 'section_2': -1}
Which seems to apply the calcs to each question rather than group the values and Sum
of those.
Ok, that’s because they’re using annotate
instead of aggregate
in that example.
What happens if you combine those two example expressions in a single aggregate
function instead of the tow annotate
calls?
Thanks again, Ken. Got this working now.