๐ Introduction
In Django, a model is the single, definitive source of information about your data. It defines the structure of your database, including fields, relationships, and constraints.
๐ In simple terms:
Model = Blueprint of a database table
Each model:
- Represents a table
- Each attribute represents a column
- Each instance represents a row
๐ฏ Why Models are Important (Exam Point)
- Handle database operations automatically
- Reduce need for SQL queries
- Provide ORM (Object Relational Mapping)
- Ensure data consistency
๐ Exam Tip:
Django follows MTV architecture (Model-Template-View) where:
- Model = Data layer
- View = Logic
- Template = UI
๐งฉ Basic Structure of a Django Model
A model is defined inside models.py
โ Example: Student Model
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
roll_no = models.IntegerField()
email = models.EmailField()
age = models.IntegerField()
๐ Explanation of Fields
| Field | Description |
|---|---|
| CharField | Stores text (requires max_length) |
| IntegerField | Stores integers |
| EmailField | Validates email format |
| AutoField | Auto-increment primary key (default) |
๐ Django automatically adds:
id = models.AutoField(primary_key=True)
โ๏ธ Model Fields with Options
You can customize fields:
name = models.CharField(max_length=100, null=False, blank=False)
Key Options:
null=Trueโ Database can store NULLblank=Trueโ Form can be emptydefaultโ Default valueunique=Trueโ No duplicates allowed
๐๏ธ String Representation (Very Important)
def __str__(self):
return self.name
๐ Why important?
- Displays readable data in admin panel
- Frequently asked in exams
๐ Installing a Model (Database Migration)
Step 1: Create Model
Step 2: Run commands
python manage.py makemigrations
python manage.py migrate
๐ What happens?
- Django creates database table automatically
๐งช Real-Life Example
Imagine:
๐ College Management System
Model = Student table
| name | roll_no | age |
|---|
Django converts Python class โ SQL table
๐ Relationships Between Models
1. One-to-Many (ForeignKey)
class Course(models.Model):
name = models.CharField(max_length=100)
class Student(models.Model):
name = models.CharField(max_length=100)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
๐ One course โ many students
2. Many-to-Many
class Subject(models.Model):
name = models.CharField(max_length=100)
class Student(models.Model):
subjects = models.ManyToManyField(Subject)
3. One-to-One
class Profile(models.Model):
student = models.OneToOneField(Student, on_delete=models.CASCADE)
๐ง Common Mistakes (Very Important)
โ Forgetting max_length in CharField
โ Not running migrations
โ Missing __str__() method
โ Confusing null and blank
๐ Viva Questions (Exam Focus)
Q1. What is a Django model?
๐ A Python class that defines database structure.
Q2. What is ORM?
๐ Object Relational Mapping โ converts Python objects into database tables.
Q3. Difference between null and blank?
nullโ database levelblankโ form validation
Q4. What is makemigrations?
๐ Creates migration file for changes.
Q5. What is migrate?
๐ Applies changes to database.
๐ป Mini Practical Program
# models.py
from django.db import models
class Employee(models.Model):
name = models.CharField(max_length=50)
salary = models.IntegerField()
def __str__(self):
return self.name
๐ Expected Output (Admin Panel)
Employee:
- Rahul
- Neha
๐ฏ Summary (Exam Revision)
- Model = Database table
- Defined in
models.py - Uses fields like CharField, IntegerField
- Requires migration
- Supports relationships
- Uses ORM (no SQL needed)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
