0Pricing
Django Academy · Lesson

form.save() and commit=False

Persist data and tweak instances before saving.

form.save() and commit=False is a free Django Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

Is the “form.save() and commit=False” lesson free?

Yes — the full text of “form.save() and commit=False” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “form.save() and commit=False”?

Persist data and tweak instances before saving. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Django Academy?

No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “form.save() and commit=False” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Django Academy lesson?

Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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