TS-07 Django Models
TS-07 Django Models
Models, Migration,
ORM
Hands-on
Tasks:
Django
Creating
Models, Understan Advanced
Relationshi
Model ding ORM
p in
Fields, Migrations Queries
Models,
Queries
Custom
Queries
A model in Django is a
class that defines the
structure of database
Models
tables. serve as a
blueprint for database
tables and define the
Django fields and behaviors of the
Models data being stored.
Importance of Models:
• Simplifies database management.
• Enables automatic generation of
queries using Django's ORM.
• Reflects real-world data and
relationships.
Creating Models
in Django
• Defining a Model
• Models are Python classes that inherit from
django.db.models.Model.
• Fields are defined as class attributes.
• Example: Basic Model
Common Field Types
Advanced Count
• avg_price =
ORM
Book.objects.aggregate(Avg('price'))
Migrations: Automatically
handle schema changes
(makemigrations, migrate).