aggregate vs. annotate
Gesamte QuerySets oder einzelne Zeilen zusammenfassen
aggregate vs. annotate ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Django Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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}Naming Your Result
Pass a keyword to aggregate to name the output key yourself, which reads far better than the auto-generated price__avg label.
Book.objects.aggregate(avg_price=Avg("price"))
# {"avg_price": 24.5}Common Aggregate Functions
You get the usual SQL toolkit: Count, Sum, Avg, Min, and Max. Each one rolls the whole queryset down to a single number.
from django.db.models import Count, Sum
Book.objects.aggregate(total=Count("id"), revenue=Sum("price"))What annotate Returns
By contrast, annotate returns a queryset where every object carries a new computed attribute, so you can keep filtering and ordering.
Per-Row Counts
Use annotate with a related count to answer per-object questions, like how many books each author has written.
Author.objects.annotate(num_books=Count("book"))
# each author now has .num_booksGroup By Happens for You
When you annotate over a relation, Django adds the GROUP BY clause automatically, grouping by the model you started from.
Filter on an Annotation
Because annotate keeps a queryset, you can filter on the new value, for example to find authors with more than five books.
Author.objects.annotate(n=Count("book")).filter(n__gt=5)Order by an Annotation
You can also order_by a computed value, so ranking your busiest authors is a one-line query.
Author.objects.annotate(n=Count("book")).order_by("-n")Filter Before You Annotate
Order matters: a filter before annotate narrows which rows get counted, while a filter after it tests the computed result.
Pick the Right Tool
Ask one question: do you want a single summary for the whole set, or one value per object? That choice decides aggregate versus annotate.
Quick Check
You want each author's book count, kept as a queryset you can still order. Which call fits?
Recap
Remember the split: aggregate returns one summary dict and ends the query, while annotate adds a value per row and keeps a queryset you can filter and order. 🎯
Häufig gestellte Fragen
Ist die Lektion „aggregate vs. annotate“ kostenlos?
Ja — der vollständige Text von „aggregate vs. annotate“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Django Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „aggregate vs. annotate“?
Gesamte QuerySets oder einzelne Zeilen zusammenfassen Du übst Django Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Django Academy zu starten?
Keine Vorkenntnisse erforderlich. Django Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „aggregate vs. annotate“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Django Academy-Lektion Code schreiben und ausführen?
Ja. Jede Django Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- aggregate vs. annotate
- F Expressions für atomare Aktualisierungen
- Q Objects für komplexe Filter
- Bedingte Aggregation mit Case/When