Defining a Model Class
Turn a Python class into a database table.
Defining a Model Class 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.
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! 🎉
Frequently asked questions
Is the “Defining a Model Class” lesson free?
Yes — the full text of “Defining a Model Class” 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 “Defining a Model Class”?
Turn a Python class into a database table. 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 “Defining a Model Class” 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
- Defining a Model Class
- Choosing Field Types
- Field Options: null, blank, default
- str and the Meta Class