التجميع الشرطي باستخدام Case/When
تطبيق المنطق الشرطي داخل استعلام واحد
التجميع الشرطي باستخدام Case/When درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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, ValueA 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. 📊
الأسئلة الشائعة
هل درس «التجميع الشرطي باستخدام Case/When» مجاني؟
نعم — نص درس «التجميع الشرطي باستخدام Case/When» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «التجميع الشرطي باستخدام Case/When»؟
تطبيق المنطق الشرطي داخل استعلام واحد تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «التجميع الشرطي باستخدام Case/When»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- aggregate مقابل annotate
- تعبيرات F للتحديثات الذرية
- كائنات Q لعوامل التصفية المعقدة
- التجميع الشرطي باستخدام Case/When