0Pricing
Django Academy · درس

ManyToManyField ونماذج Through

ربط السجلات في الاتجاهين مع بيانات إضافية

ManyToManyField ونماذج Through درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.

ماذا ستتعلم في «ManyToManyField ونماذج Through»؟

ربط السجلات في الاتجاهين مع بيانات إضافية تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟

لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «ManyToManyField ونماذج Through»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟

نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. ForeignKey و on_delete
  2. ManyToManyField ونماذج Through
  3. OneToOneField للملفات الشخصية
  4. related_name وعمليات البحث العكسية
← العودة إلى Django Academy