← Back to Django Course | Chapter 6: Advanced ORM Queries | Lesson 4 of 9

Aggregation with Count and Sum

Aggregation is like asking 'how many toys do I have in total?' instead of listing each toy one by one — it turns many rows into a single summary number.

Counting Rows

The simplest way to count matching rows is QuerySet.count(), which asks the database for a number instead of loading every row into Python.

Note: count() is faster than len(queryset) because it doesn't fetch the actual rows.

Example: Counting Rows

This returns a single integer — the number of published tutorial rows — without loading their data.

markup
from core.models import Tutorial

# How many tutorials are published?
Tutorial.objects.filter(is_published=True).count()
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Using aggregate() with Count and Sum

aggregate() runs one or more aggregation functions across a whole queryset and returns a dictionary of results.

Example: Using aggregate() with Count and Sum

This returns something like {total: 15, total_order: 120} — one dictionary summarizing the whole queryset.

markup
from django.db.models import Count, Sum
from core.models import Chapter

Chapter.objects.aggregate(
    total=Count('id'),
    total_order=Sum('order'),
)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Counting Related Objects

Count can also count how many related objects each row has, which is useful for showing 'how many tutorials does this chapter have'.

Warning: Count() on a related field only works when a ForeignKey links the two models.

Example: Counting Related Objects

Each Chapter object in the result now has a .tutorial_count attribute showing how many related Tutorial rows point to it.

markup
from django.db.models import Count
from core.models import Chapter

Chapter.objects.annotate(tutorial_count=Count('tutorials'))
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Confusing .count() (a QuerySet method that returns one number) with Count (an aggregation function used inside .aggregate() or .annotate()).
  2. Forgetting to import Count/Sum from django.db.models before using them.
  3. Using aggregate() when annotate() was needed (or vice versa) — aggregate() returns one summary for the whole queryset, annotate() adds a value to each object.
Chapter Summary
  • aggregate() computes a single summary value across an entire queryset.
  • Count and Sum are imported from django.db.models and used inside aggregate().
  • annotate() adds a computed value to each object in a queryset instead of one overall summary.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.