OneToOneField للملفات الشخصية
توسيع نموذج بسجل مقترن
OneToOneField للملفات الشخصية درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 للملفات الشخصية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «OneToOneField للملفات الشخصية»؟
توسيع نموذج بسجل مقترن تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «OneToOneField للملفات الشخصية»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- ForeignKey و on_delete
- ManyToManyField ونماذج Through
- OneToOneField للملفات الشخصية
- related_name وعمليات البحث العكسية