prefetch_related สำหรับ M2M
โหลดข้อมูลหลายต่อหลายเป็นชุดอย่างมีประสิทธิภาพ
prefetch_related สำหรับ M2M เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Meet prefetch_related
prefetch_related handles the relationships a join cannot, like many-to-many and reverse links, with smart batching. 📦
Two Queries, Not Many
Instead of one giant join, it runs a second query for the related rows, then matches them up in Python.
The Basic Call
Pass the relation name to prefetch_related, just like before, and Django batches the lookups for you automatically.
authors = Author.objects.prefetch_related('books')Why Two Beats N+1
One query for authors and one for all their books is two trips total, no matter how many authors you loop over.
for author in authors:
print(author.books.count())Best for Many
Reach for it whenever each object links to multiple rows: many-to-many fields and reverse foreign keys are perfect fits.
Reverse Relations Work
It shines on reverse accessors, like an author reaching all their books, where select_related simply cannot help.
Mixing Both Tools
You can chain both optimizers on one queryset, using each for the relationship type it handles best.
Book.objects.select_related('author').prefetch_related('tags')Control with Prefetch
The Prefetch object lets you filter or order the related rows that get fetched, giving you fine control.
from django.db.models import Prefetch
Author.objects.prefetch_related(
Prefetch('books', queryset=Book.objects.filter(year=2024)))Done in Python
The matching happens in Python, not SQL. That is why it sidesteps the row explosion a big many-to-many join would cause.
Filter Inside the Loop Carefully
Calling filter on a prefetched set inside a loop fires fresh queries again. Use Prefetch to keep the batching benefit.
Two Tools, One Goal
select_related for single links, prefetch_related for many. Together they crush N+1 in almost any view. 🚀
Quick Check
Choose the right optimizer for this case.
Recap
prefetch_related batches many-to-many and reverse links into a second query, matched in Python. Pair it with select_related for full coverage. ✅
คำถามที่พบบ่อย
บทเรียน “prefetch_related สำหรับ M2M” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “prefetch_related สำหรับ M2M” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “prefetch_related สำหรับ M2M”
โหลดข้อมูลหลายต่อหลายเป็นชุดอย่างมีประสิทธิภาพ คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “prefetch_related สำหรับ M2M” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตรวจจับปัญหา N+1
- select_related สำหรับ ForeignKey
- prefetch_related สำหรับ M2M
- การทำโปรไฟล์ด้วย Debug Toolbar