การค้นหาด้วยฟิลด์และ exclude()
สืบค้นด้วย contains, gte, in และอื่น ๆ
การค้นหาด้วยฟิลด์และ exclude() เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Beyond Equality
Plain filters check for equality, but real queries need ranges and text matches. Field lookups unlock that extra power.
The Double Underscore
A lookup is the field name, a double underscore, then the operator. That tiny syntax is how Django reads richer conditions.
Book.objects.filter(year__gte=2020)Greater and Less Than
Use gt, gte, lt, and lte for numeric or date ranges. They map straight to SQL comparison operators.
Book.objects.filter(price__lt=20)Search Inside Text
The contains lookup finds rows where a field holds your substring anywhere inside it. Great for simple searches.
Book.objects.filter(title__contains="Python")Ignore the Case
Add an i for case-insensitive text lookups like icontains or iexact, so capitals never trip up a match.
Book.objects.filter(title__icontains="python")Starts and Ends
Match the edges of a string with startswith and endswith. Both have case-insensitive twins too.
Book.objects.filter(title__startswith="The")Match a Set of Values
The in lookup keeps rows whose value appears in a list you pass. It is cleaner than chaining many OR checks.
Book.objects.filter(year__in=[2022, 2023, 2024])Check for Empty Values
Use isnull to find rows where a column is NULL, or set it False to find the rows that have a value.
Book.objects.filter(summary__isnull=True)Flip the Logic
Sometimes you want everything except a match. exclude() returns the rows that do NOT meet the condition.
Book.objects.exclude(published=True)Lookups Work in exclude Too
Any field lookup that works in filter also works in exclude(). The only difference is the result is inverted.
Book.objects.exclude(year__gte=2020)Combine filter and exclude
Chain both together to keep one group and drop another. Each call narrows the same lazy QuerySet further.
Book.objects.filter(published=True).exclude(price__gt=50)Quick Check
You want books whose title holds "django" regardless of capitalization. Which lookup fits?
Recap
You leveled up! Field lookups use the double underscore for ranges and text, and exclude() flips any condition. 🔍
คำถามที่พบบ่อย
บทเรียน “การค้นหาด้วยฟิลด์และ exclude()” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การค้นหาด้วยฟิลด์และ exclude()” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การค้นหาด้วยฟิลด์และ exclude()”
สืบค้นด้วย contains, gte, in และอื่น ๆ คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การค้นหาด้วยฟิลด์และ exclude()” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- all(), get() และ filter()
- การค้นหาด้วยฟิลด์และ exclude()
- order_by การตัดแบ่ง และการประเมินแบบขี้เกียจ
- values, values_list และ count