0
0
Djangoframework~10 mins

Defining models with fields in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Defining models with fields
Start: Define model class
Add field attributes
Set field types and options
Save model file
Run migrations to create DB table
Model ready for use in app
This flow shows how you create a Django model by defining a class with fields, then prepare it for database use.
Execution Sample
Django
from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    pages = models.IntegerField()
Defines a Book model with a title and number of pages as fields.
Execution Table
StepActionCode LineEffectResulting Model State
1Import models modulefrom django.db import modelsmodels module availableNo model defined yet
2Define Book class inheriting models.Modelclass Book(models.Model):Book model class createdEmpty Book model
3Add title field as CharFieldtitle = models.CharField(max_length=100)title field added with max_length=100Book model has title field
4Add pages field as IntegerFieldpages = models.IntegerField()pages field added as integerBook model has title and pages fields
5Save file and run migrationspython manage.py makemigrationsMigration file createdDatabase ready to create Book table
6Apply migrationspython manage.py migrateBook table created in DBModel fully ready for use
7Exit-Model defined and synced with DBReady to create Book instances
💡 Model class defined and database table created after migrations
Variable Tracker
VariableStartAfter Step 3After Step 4Final
Book modelundefinedclass with title fieldclass with title and pages fieldsclass with both fields ready for DB
Key Moments - 3 Insights
Why do we inherit from models.Model?
In step 2, inheriting from models.Model tells Django this class is a database model, enabling field definitions and DB table creation.
What does max_length=100 mean in CharField?
Step 3 shows max_length=100 limits the title field to 100 characters, which Django enforces in the database schema.
Why run makemigrations and migrate after defining fields?
Steps 5 and 6 create and apply database changes so the model fields exist as columns in the database table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what fields does the Book model have after step 4?
AOnly pages
Btitle and pages
COnly title
DNo fields yet
💡 Hint
Check the 'Resulting Model State' column at step 4 in the execution table.
At which step is the database table for the Book model created?
AStep 3
BStep 5
CStep 6
DStep 2
💡 Hint
Look for when 'Book table created in DB' appears in the 'Effect' column.
If you forget to inherit from models.Model, what happens?
ADjango won't recognize the class as a model
BModel works fine without inheritance
CFields become database columns anyway
DMigrations run without errors
💡 Hint
Refer to the key moment about inheritance and step 2 in the execution table.
Concept Snapshot
Define a Django model by creating a class inheriting from models.Model.
Add fields as class attributes using field types like CharField or IntegerField.
Set options like max_length to control field behavior.
Run makemigrations and migrate to create database tables.
Models represent database tables in your app.
Full Transcript
To define a model in Django, start by importing models from django.db. Then create a class that inherits from models.Model. Inside this class, add fields as attributes using Django's field types, such as CharField for text and IntegerField for numbers. Each field can have options like max_length to limit text size. After saving your model, run 'python manage.py makemigrations' to create migration files that describe database changes. Then run 'python manage.py migrate' to apply these changes and create the corresponding database table. This process connects your Python model class to a real database table, ready for storing data.