0Pricing
Django Academy · Lektion

form.save() und commit=False

Daten speichern und Instanzen vor dem Speichern anpassen

form.save() und commit=False ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Django Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.user

Finish 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. ✅

Häufig gestellte Fragen

Ist die Lektion „form.save() und commit=False“ kostenlos?

Ja — der vollständige Text von „form.save() und commit=False“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Django Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „form.save() und commit=False“?

Daten speichern und Instanzen vor dem Speichern anpassen Du übst Django Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Django Academy zu starten?

Keine Vorkenntnisse erforderlich. Django Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „form.save() und commit=False“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Django Academy-Lektion Code schreiben und ausführen?

Ja. Jede Django Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Ein ModelForm und Meta.fields deklarieren
  2. form.save() und commit=False
  3. Benutzerdefinierte Validierung mit clean-Methoden
  4. Widgets, Labels und Hilfetexte
← Zurück zu Django Academy