aggregate ve annotate
Sorgu kümelerinin tamamını veya satır bazında özetleyin.
aggregate ve annotate, CoddyKit'te ücretsiz bir Django Academy dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Django Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Django Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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. 🎯
Sıkça Sorulan Sorular
“aggregate ve annotate” dersi ücretsiz mi?
Evet — “aggregate ve annotate” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Django Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Django Academy kursu toplamda 4 dersten oluşur.
“aggregate ve annotate” dersinde ne öğreneceğim?
Sorgu kümelerinin tamamını veya satır bazında özetleyin. Django Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Django Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Django Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.
“aggregate ve annotate” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Django Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Django Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- aggregate ve annotate
- Atomik Güncellemeler için F İfadeleri
- Karmaşık Filtreler için Q Nesneleri
- Case/When ile Koşullu Toplama