Django — Aggregate Sum of All Fields

Well, the docs as of today on aggregation don’t mention anything about the specific types of Aggregation possible. It mentions Avg, Min, Count, etc.

Update from Derek below: actually, it’s in the docs in the Queryset API docs under “Aggregation Functions”
http://docs.djangoproject.com/en/dev/ref/models/querysets/#sum
Missed that… but there should be a link back there from the main aggregation docs.

Anyhoo, I found another type Sum in django.db.models.

Just apply
MyQueryset.aggregate(Sum('my_field')) to get the sum of all my_fields in your queryset.

5 Comments

  1. Derek says:

    Actually, I think the docs have always described this:
    http://docs.djangoproject.com/en/dev/ref/models/querysets/#sum

    1. Yuji says:

      Ahh you are right. It just wasn’t in the aggregation docs. Oops!

  2. Derek says:

    Yup. Always check the full API docs!

  3. venkatesh says:

    thanks useful to me

Leave a reply to Yuji Cancel reply