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
- Declaring a ModelForm and Meta.fields
- form.save() and commit=False
- Custom Validation with clean methods
- Widgets, Labels, and Help Text