Aggregation with Count and Sum
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.
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.
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.
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. #}
- Confusing .count() (a QuerySet method that returns one number) with Count (an aggregation function used inside .aggregate() or .annotate()).
- Forgetting to import Count/Sum from django.db.models before using them.
- Using aggregate() when annotate() was needed (or vice versa) — aggregate() returns one summary for the whole queryset, annotate() adds a value to each object.
- 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.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: