0Pricing
Django Academy · Lesson

Declaring a ModelForm and Meta.fields

Bind a form to a model in a few lines.

Declaring a ModelForm and Meta.fields is a free Django Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why ModelForm Exists

You already have a model describing your data. A ModelForm builds a form straight from that model, so you never repeat the same fields twice. 🎯

Import ModelForm

ModelForms live in the same place as regular forms. You subclass forms.ModelForm instead of forms.Form to get the model-aware version.

from django import forms
from .models import Post

The Meta Class

A ModelForm needs an inner Meta class. Meta is where you tell the form which model it is built from and which fields to include.

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

Point at a Model

The model attribute names the class your form mirrors. Django reads that model's fields to decide which form inputs to generate for you.

class Meta:
    model = Post

Listing Fields Explicitly

The fields list controls exactly which model fields become form inputs. Naming them keeps the form intentional and safe from surprises.

fields = ["title", "body", "published"]

The Shortcut: fields = all

Setting fields to the string "__all__" includes every model field. It is handy in demos but risky, since new model fields appear automatically.

class Meta:
    model = Post
    fields = "__all__"

Excluding Instead

You can flip it with exclude to keep everything except a few fields. Prefer an explicit fields list when you can, for clarity.

class Meta:
    model = Post
    exclude = ["author"]

Auto-Generated Widgets

Django picks a sensible widget for each field automatically. A CharField becomes a text box and a BooleanField becomes a checkbox without extra work.

Validation Comes Free

Your model's rules ride along too. A field marked required or with a max length brings that same validation into the form for free.

Using It in a View

In a view you create the form the same way as any form. An empty PostForm() renders blank inputs ready for the user to fill.

def create(request):
    form = PostForm()
    return render(request, "create.html", {"form": form})

Binding to an Instance

Pass an existing object with instance and the form pre-fills its values. The same ModelForm now powers both creating and editing.

form = PostForm(instance=post)

Quick Check

Where do you tell a ModelForm which model and fields to use?

Recap: ModelForm Basics

Nice work! A ModelForm builds a form from a model using an inner Meta with model and fields. You skip repetition and inherit validation for free. ✨

Frequently asked questions

Is the “Declaring a ModelForm and Meta.fields” lesson free?

Yes — the full text of “Declaring a ModelForm and Meta.fields” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “Declaring a ModelForm and Meta.fields”?

Bind a form to a model in a few lines. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Django Academy?

No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Declaring a ModelForm and Meta.fields” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Django Academy lesson?

Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Declaring a ModelForm and Meta.fields
  2. form.save() and commit=False
  3. Custom Validation with clean methods
  4. Widgets, Labels, and Help Text
← Back to Django Academy