Skip to content
Learn Netverks

Lesson

Step 23/36 64% through track

forms-model

ModelForms

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches ModelForms: the syntax, APIs, and habits you need before advancing in Django.

The ORM is Django’s core productivity lever—N+1 queries and migration mistakes show up in every senior review.

You will apply ModelForms in contexts like: Blog engines, e-commerce catalogs, and multi-tenant SaaS data layers.

Write Python 3 in the editor and click Run on server—the dev runner executes your script; Django framework lessons also use local startproject for full MVT (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

ModelForm generates HTML forms from model fields—validation and saving stay DRY. One model drives admin, API serializers, and user-facing forms.

Basic ModelForm

from django.forms import ModelForm
from .models import Article

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

View pattern

if request.method == "POST":
    form = ArticleForm(request.POST)
    if form.is_valid():
        form.save()
        return redirect("index")
else:
    form = ArticleForm()

Important interview questions and answers

  1. Q: ModelForm vs Form?
    A: ModelForm ties to a model and can save(); plain Form is for non-model data.
  2. Q: Excluding fields?
    A: Use fields or exclude—never expose sensitive fields like is_superuser.
  3. Q: CSRF?
    A: POST forms need {% csrf_token %}—Django validates the token.

Self-check

  1. What does form.is_valid() do?
  2. Why list explicit fields in Meta?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • ModelForm save flow?
  • clean() method?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump