Topic: Django Models and ORM — From Basics to Advanced Queries
---
What is a Model in Django?
• A model in Django is a Python class that defines the structure of your database table. Each model maps to a table, and each attribute represents a column.
• Django uses its ORM (Object-Relational Mapping) to interact with the database using Python code instead of SQL.
---
Creating Your First Model
---
Making Migrations
• Create and apply migrations to sync models with the database:
---
Using the Model
---
Model Field Types
• CharField, TextField, IntegerField, FloatField, DateField, DateTimeField, BooleanField, EmailField, and more.
---
Meta Class for Model Options
---
Relationships Between Models
• One-to-Many (ForeignKey)
• Many-to-Many (ManyToManyField)
• One-to-One (OneToOneField)
---
Advanced ORM Queries
---
Summary
• Django models define your database structure.
• The ORM allows you to query and manipulate data using Python.
• Supports relationships, complex filtering, ordering, and aggregation.
---
Exercise
• Create two models:
1. Add a new book.
2. List all books by a specific author.
3. Delete books published before the year 2000.
---
#Django #WebDevelopment #ORM #DatabaseModels #DjangoTips
https://t.iss.one/DataScience4
---
What is a Model in Django?
• A model in Django is a Python class that defines the structure of your database table. Each model maps to a table, and each attribute represents a column.
• Django uses its ORM (Object-Relational Mapping) to interact with the database using Python code instead of SQL.
---
Creating Your First Model
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=100)
published_date = models.DateField()
pages = models.IntegerField()
---
Making Migrations
• Create and apply migrations to sync models with the database:
python manage.py makemigrations
python manage.py migrate
---
Using the Model
# Creating a new record
book = Book(title="1984", author="George Orwell", published_date="1949-06-08", pages=328)
book.save()
# Fetching all books
books = Book.objects.all()
# Filtering
orwell_books = Book.objects.filter(author="George Orwell")
# Getting one object
book = Book.objects.get(id=1)
# Updating
book.title = "Animal Farm"
book.save()
# Deleting
book.delete()
---
Model Field Types
• CharField, TextField, IntegerField, FloatField, DateField, DateTimeField, BooleanField, EmailField, and more.
---
Meta Class for Model Options
class Book(models.Model):
title = models.CharField(max_length=200)
class Meta:
ordering = ['title'] # default ordering by title
---
Relationships Between Models
• One-to-Many (ForeignKey)
• Many-to-Many (ManyToManyField)
• One-to-One (OneToOneField)
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
---
Advanced ORM Queries
# Complex filters
books = Book.objects.filter(published_date__year__gte=2000, pages__lte=300)
# Exclude
books = Book.objects.exclude(author="J.K. Rowling")
# Ordering
books = Book.objects.order_by("-published_date")
# Count
total = Book.objects.count()
---
Summary
• Django models define your database structure.
• The ORM allows you to query and manipulate data using Python.
• Supports relationships, complex filtering, ordering, and aggregation.
---
Exercise
• Create two models:
Author and Book. Link them using a foreign key. Then, write views that:1. Add a new book.
2. List all books by a specific author.
3. Delete books published before the year 2000.
---
#Django #WebDevelopment #ORM #DatabaseModels #DjangoTips
https://t.iss.one/DataScience4
❤4