Eine Model-Klasse definieren
Machen Sie aus einer Python-Klasse eine Datenbanktabelle.
Eine Model-Klasse definieren 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.
A Class That Becomes a Table
In Django, a model is a Python class that describes one kind of data. Each model becomes a real database table for you.
Inherit from models.Model
Every model subclasses models.Model. That parent gives your class all the powers to talk to the database. 🗄️
from django.db import models
class Book(models.Model):
passFields Are Class Attributes
You describe columns by adding fields as class attributes. Each field is one column in the table Django will build.
class Book(models.Model):
title = models.CharField(max_length=200)One Field, One Column
Think of it simply: one field maps to one column. Add a field and you add a column to the table.
class Book(models.Model):
title = models.CharField(max_length=200)
pages = models.IntegerField()Models Live in models.py
You write your models inside each app's models.py file. Django knows to look there for the data shapes of that app.
The Automatic Primary Key
Django quietly adds an id field as the primary key. You get a unique identifier for every row without writing any code.
No SQL Required
You never write CREATE TABLE by hand. The ORM turns your Python class into SQL, so you stay in Python the whole time.
Class Name to Table Name
By default the table name is the app label plus the lowercased class name, like app_book. Django picks a sensible name for you.
Instances Are Rows
Each instance of your model is one row of data. Create a Book object and you are describing one book record.
book = Book(title="Dune", pages=412)Singular, CapitalizedNames
Name models in singular CamelCase: use Book, not Books. Django adds plurals where it needs them in the admin.
Migrations Make It Real
A model is just a plan until you run migrations. That step creates the matching table inside your real database.
Quick Check
Let's confirm what a Django model actually maps to.
Recap: Models in a Nutshell
You learned that a model is a class extending models.Model, its fields become columns, and migrations turn it into a real table. Nice start! 🎉
Häufig gestellte Fragen
Ist die Lektion „Eine Model-Klasse definieren“ kostenlos?
Ja — der vollständige Text von „Eine Model-Klasse definieren“ 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 „Eine Model-Klasse definieren“?
Machen Sie aus einer Python-Klasse eine Datenbanktabelle. 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 „Eine Model-Klasse definieren“?
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
- Eine Model-Klasse definieren
- Feldtypen auswählen
- Feldoptionen: null, blank, default
- str und die Meta-Klasse