Models Basics
- Introduction to Django Models, understanding fields, attributes, and how models represent database tables in Django ORM.
What are Models in Django?
A Model in Django is a Python class that represents a database table.
Each model = one table
Each attribute = one column
Each object = one row
Django uses ORM (Object Relational Mapping) to interact with the database without writing SQL queries.
Why Models are Important?
No need to write SQL manually
Database-independent (MySQL, SQLite, PostgreSQL)
Clean, readable, and reusable code
Easy CRUD operations (Create, Read, Update, Delete)
Real-Life Example
Django Model Workflow
Model (Python Class)
↓
Django ORM
↓
Database Table
Creating a Model (Basic Structure)
Basic Model Structure
This code shows how to create a Django model using models.Model.
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
Explanation
models.Model → Base class for all models
Student → Table name
name, age → Table columns
Model Fields & Data Types
Django provides many field types to define database columns.
1. CharField
Used For:
Short text (name, title, city)
CharField Example
Stores small text with a maximum length.
name = models.CharField(max_length=50)
max_length is mandatory
2. TextField
Used For:
Long text (description, address)
TextField Example
description = models.TextField()
3. IntegerField
Used For:
Whole numbers (age, quantity)
IntegerField Example
age = models.IntegerField()
4. FloatField
Used For:
Decimal values (price, rating)
FloatField Example
price = models.FloatField()
5. BooleanField
Used For:
True / False values
BooleanField Example
is_active = models.BooleanField(default=True)
6. DateField
Used For:
Date only (DOB)
DateField Example
dob = models.DateField()
7. DateTimeField
Used For:
Date + Time (created time)
DateTimeField Example
created_at = models.DateTimeField(auto_now_add=True)
auto_now_add=True → sets value once
8. EmailField
Used For:
Email addresses
EmailField Example
email = models.EmailField()
9. AutoField (Primary Key)
Used For:
Auto-generated ID
Primary Key Example
id = models.AutoField(primary_key=True)
Django adds this automatically if not defined.
Complete Model Example
Student Model
This example combines multiple fields used in real projects.
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
age = models.IntegerField()
address = models.TextField()
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
Explanation
Model to Database Connection
Steps to Apply Model in DB
python manage.py makemigrations
python manage.py migrate
Explanation
makemigrations → creates migration file
migrate → creates table in database
Viewing Model in Admin Panel
Register Model in Admin
Now model appears in Django Admin UI.
from django.contrib import admin
from .models import Student
admin.site.register(Student)