0Pricing
Django Academy · درس

prefetch_related لعلاقات M2M

تحميل بيانات العلاقات المتعددة بكفاءة على دفعات

prefetch_related لعلاقات M2M درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Meet prefetch_related

prefetch_related handles the relationships a join cannot, like many-to-many and reverse links, with smart batching. 📦

Two Queries, Not Many

Instead of one giant join, it runs a second query for the related rows, then matches them up in Python.

The Basic Call

Pass the relation name to prefetch_related, just like before, and Django batches the lookups for you automatically.

authors = Author.objects.prefetch_related('books')

Why Two Beats N+1

One query for authors and one for all their books is two trips total, no matter how many authors you loop over.

for author in authors:
    print(author.books.count())

Best for Many

Reach for it whenever each object links to multiple rows: many-to-many fields and reverse foreign keys are perfect fits.

Reverse Relations Work

It shines on reverse accessors, like an author reaching all their books, where select_related simply cannot help.

Mixing Both Tools

You can chain both optimizers on one queryset, using each for the relationship type it handles best.

Book.objects.select_related('author').prefetch_related('tags')

Control with Prefetch

The Prefetch object lets you filter or order the related rows that get fetched, giving you fine control.

from django.db.models import Prefetch
Author.objects.prefetch_related(
  Prefetch('books', queryset=Book.objects.filter(year=2024)))

Done in Python

The matching happens in Python, not SQL. That is why it sidesteps the row explosion a big many-to-many join would cause.

Filter Inside the Loop Carefully

Calling filter on a prefetched set inside a loop fires fresh queries again. Use Prefetch to keep the batching benefit.

Two Tools, One Goal

select_related for single links, prefetch_related for many. Together they crush N+1 in almost any view. 🚀

Quick Check

Choose the right optimizer for this case.

Recap

prefetch_related batches many-to-many and reverse links into a second query, matched in Python. Pair it with select_related for full coverage. ✅

الأسئلة الشائعة

هل درس «prefetch_related لعلاقات M2M» مجاني؟

نعم — نص درس «prefetch_related لعلاقات M2M» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.

ماذا ستتعلم في «prefetch_related لعلاقات M2M»؟

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

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

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

كم من الوقت يستغرق درس «prefetch_related لعلاقات M2M»؟

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

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

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

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

  1. اكتشاف مشكلة N+1
  2. select_related للمفاتيح الأجنبية
  3. prefetch_related لعلاقات M2M
  4. تحليل الأداء باستخدام Debug Toolbar
← العودة إلى Django Academy