Django

Django Models Explained with Examples (Exam Focus)

๐Ÿ“Œ 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

FieldDescription
CharFieldStores text (requires max_length)
IntegerFieldStores integers
EmailFieldValidates email format
AutoFieldAuto-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 NULL
  • blank=True โ†’ Form can be empty
  • default โ†’ Default value
  • unique=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

nameroll_noemailage

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 level
  • blank โ†’ 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)

๐Ÿš€ Next Topic

๐Ÿ‘‰ Next: Django Forms (GET & POST) with Examples

Further Reading

Introduction to Django Framework and its Features

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *