0Pricing
Django Academy · บทเรียน

การรวมแบบมีเงื่อนไขด้วย Case/When

แบ่งแขนงตรรกะภายในคิวรีเดียว

การรวมแบบมีเงื่อนไขด้วย Case/When เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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, 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. 📊

คำถามที่พบบ่อย

บทเรียน “การรวมแบบมีเงื่อนไขด้วย Case/When” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การรวมแบบมีเงื่อนไขด้วย Case/When” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การรวมแบบมีเงื่อนไขด้วย Case/When”

แบ่งแขนงตรรกะภายในคิวรีเดียว คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การรวมแบบมีเงื่อนไขด้วย Case/When” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม

ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. aggregate เทียบกับ annotate
  2. นิพจน์ F สำหรับการอัปเดตแบบอะตอมิก
  3. ออบเจ็กต์ Q สำหรับตัวกรองซับซ้อน
  4. การรวมแบบมีเงื่อนไขด้วย Case/When
← กลับไปที่ Django Academy