What is the difference between Django ModelForm and a plain Form?
Learn the difference between Django ModelForm and a plain Form: field generation, save() persistence, validation, and when to use each, with code examples.
Expected Interview Answer
A plain Django Form defines its fields by hand and is not tied to any model, while a ModelForm auto-generates its fields from a model and can save cleaned data straight to the database via save().
You reach for a Form when the input does not map to a model — a contact form, a search box, or a multi-step wizard. A ModelForm is bound to a model through its Meta class (model plus fields or exclude), so it derives field types, validators, and even model-level validation from the model definition, and its save() creates or updates a model instance. Both share the same validation pipeline (is_valid(), cleaned_data, clean_<field>()), but the ModelForm additionally runs the model's own validation and handles persistence.
- ModelForm removes boilerplate by deriving fields from the model
- ModelForm.save() persists data with no manual ORM code
- Plain Form gives full control for non-model input
- Model-level validators are reused automatically by ModelForm
- Both share the same clean/validation lifecycle
AI Mentor Explanation
A plain Form is a blank scoresheet you rule up column by column for a friendly match, deciding every field yourself. A ModelForm is the official scorecard printed to match the league's registered player database — the columns, valid names, and number ranges come pre-set from the roster, and filling it in updates the official record automatically.
Step-by-Step Explanation
Step 1
Pick the base class
Subclass forms.Form for standalone input or forms.ModelForm when the data maps to a model.
Step 2
Declare fields or Meta
A Form lists each field explicitly; a ModelForm sets Meta.model plus fields or exclude to derive them.
Step 3
Bind and validate
Instantiate with request.POST and call is_valid() — both run the same clean pipeline into cleaned_data.
Step 4
Persist the data
For a ModelForm call save() to create/update the instance; for a plain Form write your own save logic.
Step 5
Render in a template
Both expose the same rendering API ({{ form.as_p }}, field iteration) regardless of base class.
What Interviewer Expects
- Knows ModelForm derives fields from a model via Meta
- Knows ModelForm.save() persists while Form does not
- Understands both share the same validation lifecycle
- Can name a use case for each
- Mentions fields/exclude and model-level validation
Common Mistakes
- Claiming a plain Form can save to the database on its own
- Forgetting to set Meta.fields or Meta.exclude on a ModelForm
- Thinking validation differs fundamentally between the two
- Using a ModelForm for input that has no backing model
Best Answer (HR Friendly)
“A plain Form is a set of input fields you build by hand for anything, while a ModelForm is generated directly from a database model and can save the entered data automatically. Use a ModelForm when the form matches a table, and a plain Form when it does not.”
Code Example
from django import forms
from .models import Article
# Plain Form: fields declared by hand, no model tie
class ContactForm(forms.Form):
email = forms.EmailField()
message = forms.CharField(widget=forms.Textarea)
# ModelForm: fields derived from the model
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = ['title', 'body', 'published']
# In a view
def create_article(request):
form = ArticleForm(request.POST or None)
if form.is_valid():
form.save() # persists a new Article instanceFollow-up Questions
- How do you use fields vs exclude in a ModelForm Meta?
- What does ModelForm.save(commit=False) do and when is it useful?
- How does ModelForm run model-level validation?
- How would you customize a ModelForm's widgets or labels?
- Can a ModelForm include extra fields not on the model?
MCQ Practice
1. Which method persists data directly for a ModelForm?
ModelForm.save() creates or updates the bound model instance; a plain Form has no save() persistence.
2. Where does a ModelForm specify its backing model?
A ModelForm declares its model and fields/exclude in an inner Meta class.
3. What does save(commit=False) return?
commit=False builds the instance without hitting the database so you can set extra attributes before calling save().
Flash Cards
When to use a plain Form? — For input that does not map to a model — contact forms, search, wizards.
What derives ModelForm fields? — The Meta class (model + fields/exclude) reads them from the model definition.
Does a plain Form save to the DB? — No — you must write your own persistence logic; only ModelForm has save().
What does commit=False do? — Returns an unsaved model instance so you can modify it before saving.