form.save()와 commit=False
데이터를 저장하고 저장 전에 인스턴스를 조정합니다
form.save()와 commit=False은(는) CoddyKit의 무료 Django Academy 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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.userFinish 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” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Django Academy 강의 전체를 잠금 해제할 수 있습니다. Django Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“form.save()와 commit=False”에서 뭘 배우나요?
데이터를 저장하고 저장 전에 인스턴스를 조정합니다 브라우저에서 직접 실행하는 실습 코드로 Django Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Django Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Django Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“form.save()와 commit=False” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Django Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Django Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- ModelForm과 Meta.fields 선언
- form.save()와 commit=False
- clean 메서드를 사용한 맞춤 검증
- 위젯, 레이블 및 도움말 텍스트