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

ForeignKey และ on_delete

จำลองความสัมพันธ์แบบหนึ่งต่อกลุ่มและพฤติกรรมเมื่อมีการลบ

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

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

Linking Two Tables

Real data connects: a book has an author, an order has a customer. A ForeignKey is how one model points to another.

One-to-Many at a Glance

A ForeignKey models a one-to-many link. One author can have many books, but each book belongs to exactly one author.

Declaring a ForeignKey

Add a field of type ForeignKey and point it at the related model. Django stores the link as an id column behind the scenes.

class Book(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

The _id Column

In the database, an author ForeignKey becomes an author_id column holding the other row's primary key. That number is the actual link.

on_delete Is Required

Every ForeignKey needs on_delete. It tells Django what to do with this row when the row it points to is deleted.

CASCADE Deletes Children

CASCADE deletes this row too when its parent goes. Remove an author and all their books vanish with them.

author = models.ForeignKey(Author, on_delete=models.CASCADE)

PROTECT Blocks Deletion

Use PROTECT to refuse the delete while related rows exist. Django raises an error instead of removing the parent.

author = models.ForeignKey(Author, on_delete=models.PROTECT)

SET_NULL Keeps the Child

SET_NULL keeps the row but clears the link, setting the column to NULL. It needs null=True on the field to work.

author = models.ForeignKey(Author, null=True, on_delete=models.SET_NULL)

SET_DEFAULT and DO_NOTHING

SET_DEFAULT swaps in the field's default, while DO_NOTHING leaves the database to decide. Reach for these only in special cases.

Accessing the Related Object

From a book instance you read the linked author straight through the field. Django fetches that row for you on demand.

book = Book.objects.first()
print(book.author.name)

Filtering Across the Link

Span the relationship in queries with the double underscore. Reach into the related model's fields directly from filter.

Book.objects.filter(author__name="Ada")

Quick Check

You delete an author but want their books to stay, just unlinked. Which on_delete value fits?

Recap

Nice work! A ForeignKey models one-to-many links, and on_delete decides each row's fate when its parent is removed. 🔗

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

บทเรียน “ForeignKey และ on_delete” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “ForeignKey และ on_delete”

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

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

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

บทเรียน “ForeignKey และ on_delete” ใช้เวลานานแค่ไหน

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

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

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

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

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