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