Ein ModelForm und Meta.fields deklarieren
Ein Formular mit wenigen Zeilen an ein Model binden
Ein ModelForm und Meta.fields deklarieren ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Django Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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 PostThe 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 = PostListing 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. ✨
Lerne Python mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 30
- Lektionen
- 120
Häufig gestellte Fragen
Ist die Lektion „Ein ModelForm und Meta.fields deklarieren“ kostenlos?
Ja — der vollständige Text von „Ein ModelForm und Meta.fields deklarieren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Django Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Ein ModelForm und Meta.fields deklarieren“?
Ein Formular mit wenigen Zeilen an ein Model binden Du übst Django Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Django Academy zu starten?
Keine Vorkenntnisse erforderlich. Django Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Ein ModelForm und Meta.fields deklarieren“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Django Academy-Lektion Code schreiben und ausführen?
Ja. Jede Django Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Ein ModelForm und Meta.fields deklarieren
- form.save() und commit=False
- Benutzerdefinierte Validierung mit clean-Methoden
- Widgets, Labels und Hilfetexte