OneToOneField สำหรับโปรไฟล์
ขยายโมเดลด้วยระเบียนที่จับคู่กัน
OneToOneField สำหรับโปรไฟล์ เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Exactly One Each Way
Sometimes a row pairs with exactly one other row, both ways. That is a one-to-one relationship, perfect for splitting data cleanly.
The Profile Pattern
The classic use is a user profile: each user gets one profile, and each profile belongs to one user. Extra fields live separately.
Declaring OneToOneField
Add a OneToOneField pointing at the other model. It behaves like a ForeignKey with a built-in uniqueness guarantee.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)Unique Under the Hood
A OneToOneField is really a ForeignKey marked unique. That constraint stops two profiles from ever sharing one user.
on_delete Still Applies
Like any link, it needs on_delete. With CASCADE, deleting the user removes their profile too in one step.
user = models.OneToOneField(User, on_delete=models.CASCADE)Why Not Just Add Fields?
You could pile fields onto the original model, but a separate profile keeps it tidy and avoids touching tables you do not own.
Forward Access
From the profile, read the linked user straight through the field. It returns the single connected object.
profile = Profile.objects.first()
print(profile.user.username)Reverse Access
The reverse side is special: from a user you reach the profile with a lowercase model name, no _set suffix needed.
user = User.objects.first()
print(user.profile.bio)Creating a Profile
Make a profile by passing the user it pairs with. The unique constraint blocks a second profile for that same user.
Profile.objects.create(user=user, bio="Hello!")Auto-Creating with Signals
A common trick is a post_save signal that builds a profile the moment a user is created, so the pair always exists.
Querying Across the Pair
Filter through the link with the double underscore, reaching the profile's fields from the user side or vice versa.
User.objects.filter(profile__bio__icontains="django")Quick Check
Each user must have at most one profile, and each profile one user. Which field models this?
Recap
Great job! A OneToOneField pairs two rows uniquely, and the reverse is reached by the model's lowercase name. 👤
คำถามที่พบบ่อย
บทเรียน “OneToOneField สำหรับโปรไฟล์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “OneToOneField สำหรับโปรไฟล์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “OneToOneField สำหรับโปรไฟล์”
ขยายโมเดลด้วยระเบียนที่จับคู่กัน คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “OneToOneField สำหรับโปรไฟล์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ForeignKey และ on_delete
- ManyToManyField และโมเดล Through
- OneToOneField สำหรับโปรไฟล์
- related_name และการค้นหาย้อนกลับ