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

select_related สำหรับ ForeignKey

JOIN แถวที่เกี่ยวข้องในการคิวรีเดียว

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

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

Meet select_related

select_related tells Django to fetch related rows up front in the same query, so loops stop firing extra trips. ⚡

It Uses a JOIN

Under the hood it adds a SQL JOIN, pulling the parent and its related row together in one efficient round trip.

The Basic Call

Pass the related field name to select_related on your queryset, and the join happens automatically when the data loads.

books = Book.objects.select_related('author')

From 101 to 1

Now looping over those books and reading each author runs a single query instead of one hundred and one. Same code, huge win.

for book in books:
    print(book.author.name)

When It Fits

Use it for ForeignKey and OneToOne fields, where each object points to exactly one related row that a join can grab.

Not for Many

It does not work for many-to-many or reverse one-to-many links, because a join cannot fold many rows into one cleanly.

Follow the Chain

You can span multiple hops with double underscores, joining across more than one foreign key in a single query.

Book.objects.select_related('author__publisher')

Several at Once

Pass several field names to grab multiple relations together, all stitched into the same optimized query.

Order.objects.select_related('customer', 'product')

It Is Lazy Too

The query still waits until you actually use the data, so chaining filters and order_by before it stays cheap and flexible.

Mind the Width

Joins add columns, so do not over-select. Grab only the relations you truly read to keep each row lean.

A Reflex Worth Building

See a foreign key accessed in a loop? Reach for select_related first. It becomes second nature fast. 🎯

Quick Check

Pick the right tool for this relationship.

Recap

select_related joins ForeignKey and OneToOne data in one query, killing N+1 for single-row links. Chain hops with double underscores. ✅

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

บทเรียน “select_related สำหรับ ForeignKey” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “select_related สำหรับ ForeignKey”

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

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

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

บทเรียน “select_related สำหรับ ForeignKey” ใช้เวลานานแค่ไหน

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

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

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

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

  1. การตรวจจับปัญหา N+1
  2. select_related สำหรับ ForeignKey
  3. prefetch_related สำหรับ M2M
  4. การทำโปรไฟล์ด้วย Debug Toolbar
← กลับไปที่ Django Academy