0PricingLogin
Django Academy · Lesson

form.save() and commit=False

Persist data and tweak instances before saving.

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)

All lessons in this course

  1. Declaring a ModelForm and Meta.fields
  2. form.save() and commit=False
  3. Custom Validation with clean methods
  4. Widgets, Labels, and Help Text
← Back to Django Academy