ManyToManyField และโมเดล Through
เชื่อมระเบียนเข้าหากันได้ทั้งสองทางพร้อมข้อมูลเพิ่มเติม
ManyToManyField และโมเดล Through เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Both Sides Have Many
Sometimes each side links to many of the other: a post has many tags, a tag marks many posts. That is a many-to-many relationship.
Declaring ManyToManyField
Add a ManyToManyField pointing at the other model. Django handles the link from both directions automatically.
class Post(models.Model):
tags = models.ManyToManyField(Tag)The Hidden Join Table
Behind the scenes Django builds a join table holding pairs of ids. No new column lands on either of your two models.
Adding Relations
Once a post is saved, attach tags with add. You can pass several objects at once in a single call.
post.tags.add(python_tag, web_tag)Removing and Clearing
Drop a single link with remove, or wipe them all with clear. The related objects themselves stay safe.
post.tags.remove(web_tag)
post.tags.clear()Reading the Set
Access linked rows through the field like a normal manager. Call all to loop over everything connected.
for tag in post.tags.all():
print(tag.name)When the Link Needs Data
What if a membership has its own facts, like a join date? A plain join table cannot hold extra columns, so you need more control.
The through Model
A through model is your own table for the relationship. It lets each link carry extra fields you define.
members = models.ManyToManyField(User, through="Membership")Defining the through Class
The through model holds a ForeignKey to each side plus any extra fields. Those columns describe the connection itself.
class Membership(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
group = models.ForeignKey(Group, on_delete=models.CASCADE)
joined = models.DateField()add Stops Working
With a custom through model, plain add is disabled. You create the link by making a through row directly instead.
Membership.objects.create(user=u, group=g, joined=today)Querying Across M2M
Filter through a many-to-many link with the double underscore, just like a ForeignKey. The join table is handled for you.
Post.objects.filter(tags__name="python")Quick Check
Your group membership needs to record when each user joined. What do you reach for?
Recap
Well done! A ManyToManyField links both sides freely, and a through model lets each connection carry its own data. 🏷️
คำถามที่พบบ่อย
บทเรียน “ManyToManyField และโมเดล Through” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ManyToManyField และโมเดล Through” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ManyToManyField และโมเดล Through”
เชื่อมระเบียนเข้าหากันได้ทั้งสองทางพร้อมข้อมูลเพิ่มเติม คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ManyToManyField และโมเดล Through” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ForeignKey และ on_delete
- ManyToManyField และโมเดล Through
- OneToOneField สำหรับโปรไฟล์
- related_name และการค้นหาย้อนกลับ