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

related_name และการค้นหาย้อนกลับ

สำรวจความสัมพันธ์ได้ทั้งสองทิศทาง

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

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

Links Go Both Ways

You set a ForeignKey from book to author, but you can also walk it backward. A reverse lookup reaches the many side from the one side.

The Default _set Manager

By default Django names the reverse accessor modelname_set. From an author, the books appear as book_set.

author = Author.objects.first()
author.book_set.all()

_set Is a Manager

That book_set is a full manager, so you can chain filter, order_by, and count on it just like any query.

author.book_set.filter(published=True).count()

Naming with related_name

The _set name is clunky. Set related_name on the ForeignKey to pick a clearer reverse accessor yourself.

author = models.ForeignKey(Author, related_name="books", on_delete=models.CASCADE)

A Cleaner Reverse

Now the reverse reads naturally. Instead of book_set you simply write books, which is far easier to follow.

author.books.all()

Reverse in Queries

related_name also drives filter across the link. Use the chosen name with the double underscore from the other model.

Author.objects.filter(books__published=True)

The Default Query Name

If you skip related_name, the reverse query name is the lowercase model name, like author.objects.filter(book__title=...).

Avoiding Name Clashes

Two ForeignKeys from one model to the same target collide on the reverse name. A distinct related_name on each fixes it.

writer = models.ForeignKey(User, related_name="written", on_delete=models.CASCADE)
editor = models.ForeignKey(User, related_name="edited", on_delete=models.CASCADE)

Turning Off the Reverse

Set related_name to a value ending in + to disable the reverse accessor entirely when you never need it.

owner = models.ForeignKey(User, related_name="+", on_delete=models.CASCADE)

related_query_name

You can split the names: related_query_name sets the filter name separately from the attribute used to read related rows.

tags = models.ManyToManyField(Tag, related_name="posts", related_query_name="post")

Works for Every Relation

related_name applies to ForeignKey, ManyToManyField, and OneToOneField alike, so every reverse path can read clearly.

Quick Check

You set related_name="books" on Book's author ForeignKey. How do you list an author's books?

Recap

You did it! Reverse lookups walk a link backward, and related_name turns clunky _set accessors into clear, readable names. 🔄

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

บทเรียน “related_name และการค้นหาย้อนกลับ” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “related_name และการค้นหาย้อนกลับ”

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

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

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

บทเรียน “related_name และการค้นหาย้อนกลับ” ใช้เวลานานแค่ไหน

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

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

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

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

  1. ForeignKey และ on_delete
  2. ManyToManyField และโมเดล Through
  3. OneToOneField สำหรับโปรไฟล์
  4. related_name และการค้นหาย้อนกลับ
← กลับไปที่ Django Academy