Monday, September 24, 2018

Multiple annotations in one query

Hi

I understand that performing multiple annotations on a queryset will produce incorrect results, I was wondering how anyone else gets around this?

"Combining multiple aggregations with annotate() will yield the wrong results because joins are used instead of subqueries"

My query for example:

data = Client.objects.filter(
case=self.kwargs['case_pk']
).select_related(
'case', 'inheritance',
).prefetch_related(
'children_set', 'stepchildren_set',
'grandchildren_set', 'property_set',
'investment_set', 'insurance_set',
'liability_set', 'lumpsumdeathinservice_set',
).annotate(
property_sum=Sum('property__value',),
investment_sum=Sum('investment__value'),
insurance_sum=Sum('insurance__value'),
liability_sum=Sum('liability__value'),
lumpsumdeathinservice_sum=Sum('lumpsumdeathinservice__value'),
).order_by(
'last_name', 'first_name'
)


I need to get the sum'd values.

Will I have to query each model individually and annotate it? and then somehow merge the querysets?

Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/963bba41-6bcb-43b5-bd34-9ae4b9f709d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment