ModelForm Save with commit=False

A snippet showing how to use commit=False when saving ModelForms.


# models.py

from django.db import models

class Article(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    author = models.ForeignKey("auth.User", on_delete=models.CASCADE)
    published = models.BooleanField(default=False)
      

# forms.py

from django import forms
from .models import Article

class ArticleForm(forms.ModelForm):
    class Meta:
        model = Article
        fields = ["title", "body"]
      

# views.py

from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .forms import ArticleForm

@login_required
def create_article(request):
    if request.method == "POST":
        form = ArticleForm(request.POST)
        if form.is_valid():
            article = form.save(commit=False)  # don't write to DB yet
            article.author = request.user
            article.published = True
            article.save()
            return redirect("article-list")
    else:
        form = ArticleForm()
    return render(request, "article_form.html", {"form": form})
      
Explanation:
  • commit=False returns an unsaved instance; set attributes and call save() yourself.
  • Category Forms & Validation
  • Total Views 562
  • Last Modified 12 November, 2025
  • Tags #forms #modelform #save #commit
Previous snippet
Formsets Example
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.