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

ออบเจ็กต์ Q สำหรับตัวกรองซับซ้อน

รวมเงื่อนไขด้วย AND, OR และ NOT

ออบเจ็กต์ Q สำหรับตัวกรองซับซ้อน เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Beyond Simple filter()

Stacking keyword arguments in filter always means AND. To express OR or NOT, you need a richer tool.

Meet the Q Object

A Q object wraps a condition so you can combine conditions with boolean operators inside a single query.

from django.db.models import Q

OR with the Pipe

Combine two Q objects with the pipe operator to build an OR, matching rows that satisfy either condition.

Book.objects.filter(Q(title__icontains="django") | Q(title__icontains="flask"))

AND with the Ampersand

The ampersand joins Q objects with AND, useful when you want explicit grouping rather than plain keyword stacking.

Book.objects.filter(Q(price__lt=20) & Q(in_stock=True))

Negate with the Tilde

Put a tilde in front of a Q object to express NOT, matching every row that fails the condition.

Book.objects.filter(~Q(author__name="Anon"))

Mixing Q and Keywords

You may pass Q objects and plain keyword arguments together, but every positional Q must come before any keyword.

Book.objects.filter(Q(price__lt=20) | Q(on_sale=True), in_stock=True)

Grouping with Parentheses

Wrap combined Q objects in parentheses to control precedence, just like grouping conditions in raw SQL.

Book.objects.filter((Q(a=1) | Q(b=2)) & Q(c=3))

Build Filters Dynamically

Because Q objects are values, you can build them up conditionally in Python before passing the result to filter.

q = Q(active=True)
if term:
    q &= Q(name__icontains=term)
User.objects.filter(q)

Combine Many with reduce

Need an OR across a list of terms? Fold them together with operator.or_ to produce one combined Q.

from functools import reduce
import operator
reduce(operator.or_, [Q(tag=t) for t in tags])

Still One SQL Query

However complex the logic, Django compiles your Q tree into a single WHERE clause, so there is no extra round trip.

When to Use Q

Reach for Q whenever a filter needs OR, NOT, or logic assembled at runtime; plain keywords stay fine for simple ANDs.

Quick Check

You want books whose title contains django OR flask. Which filter expresses that?

Recap

Q objects unlock OR, AND, and NOT logic you can group and build dynamically, all compiled into one query. 🧩

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

บทเรียน “ออบเจ็กต์ Q สำหรับตัวกรองซับซ้อน” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “ออบเจ็กต์ Q สำหรับตัวกรองซับซ้อน”

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

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

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

บทเรียน “ออบเจ็กต์ Q สำหรับตัวกรองซับซ้อน” ใช้เวลานานแค่ไหน

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

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

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

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

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