1👍
✅
The problem is that you are calling the serializer with total
which is a non-iterable type, namely a number.
From the docs:
aggregate() is a terminal clause for a QuerySet that, when invoked, returns a dictionary of name-value pairs. The name is an identifier for the aggregate value; the value is the computed aggregate.
In your case:
OutgoingInvoice.objects.filter(organization_id=user_pk, status_id__in=[2,3]).aggregate(total=Sum('total_invoice_amount', field="total_invoice_amount"))
# >> {'total': 123.5} (example value)
I don’t know what you want your serializer to show but if you are trying to have the sum of the amounts for each user you may do something like this:
User.objects.annotate(total=Sum("outgoing_invoice_set__total_invoice_amount")).filter(pk=user_pk, status_id__in=[2, 3])
Source:stackexchange.com