0Pricing
Django Academy · 课时

form.save() 与 commit=False

持久化数据,并在保存前调整实例

form.save() 与 commit=False 是 CoddyKit 上的免费 Django Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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.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. ✅

常见问题解答

「form.save() 与 commit=False」课时是免费的吗?

是的 — 「form.save() 与 commit=False」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。

「form.save() 与 commit=False」这节课中我会学到什么?

持久化数据,并在保存前调整实例 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Django Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「form.save() 与 commit=False」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Django Academy 课中编写并运行代码吗?

能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 声明 ModelForm 与 Meta.fields
  2. form.save() 与 commit=False
  3. 使用 clean 方法进行自定义验证
  4. 控件、标签和帮助文本
← 返回 Django Academy