0Pricing
Django Academy · Ders

Case/When ile Koşullu Toplama

Tek bir sorgunun içinde dallanma mantığı oluşturun.

Case/When ile Koşullu Toplama, CoddyKit'te ücretsiz bir Django Academy dersidir. Bu, 4 dersinin 4. 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.

Branching Inside a Query

Sometimes you need different results for different rows. Case lets the database choose a value per row, like an if statement in SQL.

The Case and When Pair

A Case holds one or more When branches; each When pairs a condition with the value to use when that condition is true.

from django.db.models import Case, When, Value

A Simple Label

Annotate a computed label by giving each When a condition and a then value, with a default for the rest.

Book.objects.annotate(tier=Case(
  When(price__gte=50, then=Value("premium")),
  default=Value("standard")))

Why Conditional Aggregation

The real power comes from putting Case inside an aggregate, so you can count or sum only the rows that match a condition.

Count Rows That Match

Wrap a Case in Count to tally just the matching rows, since non-matching branches return null and are skipped.

Count(Case(When(active=True, then=1)))

The filter= Shortcut

Modern Django offers a cleaner way: pass filter to an aggregate to count or sum a subset without writing Case yourself.

Count("id", filter=Q(active=True))

Multiple Buckets at Once

Annotate several conditional aggregates together to build a breakdown, like active and inactive counts in one query.

Team.objects.annotate(
  active=Count("member", filter=Q(member__active=True)),
  inactive=Count("member", filter=Q(member__active=False)))

Sum a Subset

The same trick works with Sum, letting you total only the rows that pass a condition, such as paid invoices.

Sum("amount", filter=Q(status="paid"))

Set an output_field

When the value types are not obvious, give Case an output_field so Django knows how to handle the result.

Case(When(...), default=Value(0), output_field=IntegerField())

Order by a Case

You can even order_by a Case annotation to create custom sort priorities that plain field ordering cannot express.

One Trip to the Database

All of this branching runs server-side in a single query, far faster than looping in Python and counting by hand.

Quick Check

You want one number: how many members of a team are active, computed in the database.

Recap

Case and When add per-row branching, and pairing conditions with aggregates, or the filter= shortcut, builds rich breakdowns in one query. 📊

Sıkça Sorulan Sorular

“Case/When ile Koşullu Toplama” dersi ücretsiz mi?

Evet — “Case/When ile Koşullu Toplama” 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.

“Case/When ile Koşullu Toplama” dersinde ne öğreneceğim?

Tek bir sorgunun içinde dallanma mantığı oluşturun. 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 4. dersidir.

“Case/When ile Koşullu Toplama” 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

  1. aggregate ve annotate
  2. Atomik Güncellemeler için F İfadeleri
  3. Karmaşık Filtreler için Q Nesneleri
  4. Case/When ile Koşullu Toplama
← Django Academy Sayfasına Dön