0Pricing
Django Academy · 课时

aggregate 与 annotate

汇总整个查询集或逐行汇总

aggregate 与 annotate 是 CoddyKit 上的免费 Django Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Django Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Django Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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_books

Group 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. 🎯

常见问题解答

「aggregate 与 annotate」课时是免费的吗?

是的 — 「aggregate 与 annotate」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。

「aggregate 与 annotate」这节课中我会学到什么?

汇总整个查询集或逐行汇总 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Django Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「aggregate 与 annotate」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Django Academy 课中编写并运行代码吗?

能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. aggregate 与 annotate
  2. 使用 F 表达式进行原子更新
  3. 使用 Q 对象进行复杂过滤
  4. 使用 Case/When 进行条件聚合
← 返回 Django Academy