[Answered ]-Getting SUM for each unique fields in Django Query

1👍

You can .annotate(…) [Django-doc] the Party objects with:

from django.db.models import Sum

Party.objects.annotate(
    sales=Sum('sales__salestransaction__total_cost')
)

The Party objects that arise from this QuerySet will have an extra attribute sales with the total amount of sales for that Party.

Leave a comment