0PricingLogin
Django Academy · Lesson

aggregate vs annotate

Summarize whole querysets or per-row.

Two Ways to Summarize

The ORM gives you two summary tools: aggregate collapses a whole queryset into one result, while annotate adds a value to each row.

What aggregate Returns

Calling aggregate ends the query and hands you a plain Python dictionary, not a queryset, so there is nothing left to filter afterward.

from django.db.models import Avg
Book.objects.aggregate(Avg("price"))
# {"price__avg": 24.5}

All lessons in this course

  1. aggregate vs annotate
  2. F Expressions for Atomic Updates
  3. Q Objects for Complex Filters
  4. Conditional Aggregation with Case/When
← Back to Django Academy