form.save() و commit=False
حفظ البيانات وتعديل النسخ قبل الحفظ
form.save() و commit=False درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Saving in One Line
The big payoff of a ModelForm is save(). After a form validates, one call writes the data straight to the database as a new row. 💾
if form.is_valid():
form.save()save() Returns the Object
Calling save() hands back the saved model instance. Grab it when you want to redirect to the new object or read its fresh id.
post = form.save()
print(post.id)Validate Before You Save
Always gate save() behind is_valid(). Saving an invalid form raises an error, so check validity first every single time.
if form.is_valid():
form.save()Editing an Existing Row
When the form was built with an instance, save() updates that row instead of creating a new one. Same method, different outcome.
form = PostForm(request.POST, instance=post)
if form.is_valid():
form.save()Sometimes You Need a Pause
Often a model has a field the form should not collect, like the author. You need to set it yourself before the row hits the database.
Enter commit=False
Calling save(commit=False) builds the model instance from the form but does not write it to the database yet. You hold it in memory.
post = form.save(commit=False)Set Fields the Form Skipped
With the unsaved instance in hand, you can attach extra data. Here we set the author from the logged-in user before saving.
post = form.save(commit=False)
post.author = request.userFinish the Save
After tweaking the instance, call its own save() method to commit it. Now both the form data and your extra field land together.
post.author = request.user
post.save()The Full Pattern
This three-step commit=False pattern is the everyday way to mix form input with server-set values cleanly and safely.
post = form.save(commit=False)
post.author = request.user
post.save()Many-to-Many Caveat
One gotcha: with commit=False, many-to-many links are not saved yet. Call save_m2m() after saving the instance to attach them.
post.save()
form.save_m2m()Redirect After Success
Once the row is saved, send the user onward. A redirect after a successful POST prevents accidental double submissions.
post.save()
return redirect("post_detail", pk=post.pk)Quick Check
What does save(commit=False) give you the chance to do?
Recap: Saving Forms
Great progress! After is_valid(), save() writes the row. Use commit=False to set extra fields first, then save the instance, and save_m2m() if needed. ✅
الأسئلة الشائعة
هل درس «form.save() و commit=False» مجاني؟
نعم — نص درس «form.save() و commit=False» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «form.save() و commit=False»؟
حفظ البيانات وتعديل النسخ قبل الحفظ تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «form.save() و commit=False»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تعريف ModelForm و Meta.fields
- form.save() و commit=False
- التحقق المخصص باستخدام أساليب clean
- Widgets والعناوين ونصوص المساعدة