Code With Python
39.2K subscribers
887 photos
27 videos
22 files
769 links
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
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

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
Topic: Django ORM – Advanced Queries, Aggregations, and Query Optimization (Part 2)

---

1. Aggregation Functions

• Django provides built-in functions for aggregating data.

from django.db.models import Avg, Sum, Max, Min, Count

# Average number of pages
avg_pages = Book.objects.aggregate(Avg("pages"))

# Total number of pages
total_pages = Book.objects.aggregate(Sum("pages"))

# Count of books per author
book_counts = Book.objects.values("author").annotate(total=Count("id"))


---

2. Grouping and Annotating

annotate() is used to compute values for each row (e.g., totals per group).

# Number of books per author
from django.db.models import Count

authors = Author.objects.annotate(book_count=Count("book"))
for author in authors:
print(author.name, author.book_count)


---

3. Complex Lookups with Q Objects

• Use Q for OR, AND, and NOT conditions.

from django.db.models import Q

# Books with title containing 'war' OR author name 'Leo Tolstoy'
books = Book.objects.filter(Q(title__icontains="war") | Q(author__name="Leo Tolstoy"))

# Books not published in 2023
books = Book.objects.filter(~Q(published_date__year=2023))


---

4. Selecting Specific Fields

• Use values() or values\_list() to retrieve specific fields.

# Dictionary of titles and authors
data = Book.objects.values("title", "author__name")

# List of titles
titles = Book.objects.values_list("title", flat=True)


---

5. Related Model Queries

• Use select\_related and prefetch\_related to optimize related data access.

# Optimized: Single JOIN query for ForeignKey
books = Book.objects.select_related("author")

# For ManyToMany or reverse relations
authors = Author.objects.prefetch_related("book_set")


---

6. Raw SQL Queries (When Necessary)

books = Book.objects.raw("SELECT * FROM myapp_book WHERE pages > %s", [300])
for book in books:
print(book.title)


---

7. Performance Tips

• Use only() or defer() to limit retrieved fields.

books = Book.objects.only("title")


• Avoid chaining queries in loops.

• Use bulk\_create, bulk\_update for inserting/updating many records.

---

Summary

• Use aggregate(), annotate(), and Q objects for powerful filtering.

• Fetch only what you need using values, only, and select\_related.

• Optimize queries by reducing database hits and using Django’s ORM efficiently.

---

Exercise

• Write a Django query that returns all authors with more than 5 books, sorted by the number of books (descending). Then print their name and book count.

---

#Django #ORM #AdvancedQueries #QueryOptimization #WebDevelopment

https://t.iss.one/DataScience4
2👍2
Topic: Django ORM – Transactions, Subqueries, and Custom Managers (Part 3)

---

1. Working with Transactions

• Django supports atomic transactions to ensure database integrity — either all operations succeed, or none do.

from django.db import transaction

@transaction.atomic
def create_author_and_book():
author = Author.objects.create(name="New Author")
Book.objects.create(title="New Book", author=author)


• Use atomic() as a decorator or context manager.

with transaction.atomic():
# multiple operations that must succeed together
...


---

2. Subqueries and OuterRef

• Use Subquery and OuterRef to perform queries that depend on other queries.

from django.db.models import Subquery, OuterRef

# Get latest book for each author
latest_books = Book.objects.filter(author=OuterRef('pk')).order_by('-published_date')
authors = Author.objects.annotate(latest_book=Subquery(latest_books.values('title')[:1]))


---

3. Exists() and Conditional Logic

• Use Exists for optimized existence checks.

from django.db.models import Exists

recent_books = Book.objects.filter(published_date__year=2023)
authors = Author.objects.annotate(has_recent_books=Exists(recent_books.filter(author=OuterRef('pk'))))


---

4. Custom Model Managers

• Add custom query logic to models via custom managers.

from django.db import models

class PublishedBookManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(is_published=True)

class Book(models.Model):
title = models.CharField(max_length=200)
is_published = models.BooleanField(default=False)

objects = models.Manager() # Default manager
published = PublishedBookManager() # Custom manager


# Usage
Book.published.all()


---

5. QuerySet Methods: Update, Delete, Bulk Operations

update() modifies multiple records efficiently.

Book.objects.filter(author__name="Alice").update(pages=300)


delete() removes objects in bulk.

Book.objects.filter(published_date__year__lt=2000).delete()


bulk\_create() inserts many records at once.

Book.objects.bulk_create([
Book(title="Book A", author=author),
Book(title="Book B", author=author),
])


---

6. Using Database Functions

• Django provides built-in SQL functions like Lower, Upper, Length, Concat, etc.

from django.db.models.functions import Upper

books = Book.objects.annotate(upper_title=Upper('title'))


---

Summary

• Use transactions to maintain data integrity.

• Leverage subqueries, OuterRef, and Exists for complex logic.

• Create custom managers to encapsulate reusable query logic.

• Apply bulk operations and DB functions for performance and flexibility.

---

Exercise

• Create a custom manager for the Book model to return only books published in the last 5 years. Then use this manager in a view to list all recent books along with their authors.

---

#Django #ORM #Transactions #Subqueries #CustomManagers #AdvancedDjango

https://t.iss.one/DataScience4
5
# Django ORM Comparison - Know both frameworks
# Django model (contrast with SQLAlchemy)
from django.db import models

class Department(models.Model):
name = models.CharField(max_length=50)

class Employee(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField(unique=True)
department = models.ForeignKey(Department, on_delete=models.CASCADE)

# Django query (similar but different syntax)
Employee.objects.filter(department__name="HR").select_related('department')


# Async ORM - Modern Python requirement
# Requires SQLAlchemy 1.4+ and asyncpg
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession

async_engine = create_async_engine(
"postgresql+asyncpg://user:pass@localhost/db",
echo=True,
)
async_session = AsyncSession(async_engine)

async with async_session.begin():
result = await async_session.execute(
select(Employee).where(Employee.name == "Alice")
)
employee = result.scalar_one()


# Testing Strategies - Interview differentiator
from unittest import mock

# Mock database for unit tests
with mock.patch('sqlalchemy.create_engine') as mock_engine:
mock_conn = mock.MagicMock()
mock_engine.return_value.connect.return_value = mock_conn

# Test your ORM-dependent code
create_employee("Test", "[email protected]")
mock_conn.execute.assert_called()


# Production Monitoring - Track slow queries
from sqlalchemy import event

@event.listens_for(engine, "before_cursor_execute")
def before_cursor(conn, cursor, statement, params, context, executemany):
conn.info.setdefault('query_start_time', []).append(time.time())

@event.listens_for(engine, "after_cursor_execute")
def after_cursor(conn, cursor, statement, params, context, executemany):
total = time.time() - conn.info['query_start_time'].pop(-1)
if total > 0.1: # Log slow queries
print(f"SLOW QUERY ({total:.2f}s): {statement}")


# Interview Power Move: Implement caching layer
from functools import lru_cache

class CachedEmployeeRepository(EmployeeRepository):
@lru_cache(maxsize=100)
def get_by_id(self, employee_id):
return super().get_by_id(employee_id)

def invalidate_cache(self, employee_id):
self.get_by_id.cache_clear()

# Reduces database hits by 70% in read-heavy applications


# Pro Tip: Schema versioning in CI/CD pipelines
# Sample .gitlab-ci.yml snippet
deploy_db:
stage: deploy
script:
- alembic upgrade head
- pytest tests/db_tests.py # Verify schema compatibility
only:
- main


# Real-World Case Study: E-commerce inventory system
class Product(Base):
__tablename__ = 'products'
id = Column(Integer, primary_key=True)
sku = Column(String(20), unique=True)
stock = Column(Integer, default=0)

# Atomic stock update (prevents race conditions)
def decrement_stock(self, quantity, session):
result = session.query(Product).filter(
Product.id == self.id,
Product.stock >= quantity
).update({"stock": Product.stock - quantity})
if not result:
raise ValueError("Insufficient stock")

# Usage during checkout
product.decrement_stock(2, session)


By: @DATASCIENCE4 🔒

#Python #ORM #SQLAlchemy #Django #Database #BackendDevelopment #CodingInterview #WebDevelopment #TechJobs #SystemDesign #SoftwareEngineering #DataEngineering #CareerGrowth #APIs #Microservices #DatabaseDesign #TechTips #DeveloperTools #Programming #CareerTips
3
• Fetch related ForeignKey objects in the same query.
entries = Entry.objects.select_related('author').all()

• Fetch related ManyToManyField objects in a separate efficient query.
entries = Entry.objects.prefetch_related('tags').all()

• Load only specific model fields.
entries = Entry.objects.only('headline')

• Defer loading of specific model fields.
entries = Entry.objects.defer('body_text')

• Execute raw, unmanaged SQL.
authors = Author.objects.raw('SELECT * FROM myapp_author')

• Get results as a list of tuples.
Entry.objects.values_list('headline', 'pub_date')


XV. Transactions

• Import the transaction module.
from django.db import transaction

• Run a block of code within a database transaction.
with transaction.atomic():
# All database operations here are either committed together or rolled back.
author.save()
entry.save()


XVI. Managers & Model Methods

• Create a custom Manager for common queries.
class PublishedEntryManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status='published')

• Add a custom method to a QuerySet via its Manager.
Entry.objects.get_queryset().by_author("John Doe")

• Add a custom method to a model for object-specific logic.
class Entry(models.Model):
#...
def is_recent(self):
return self.pub_date > timezone.now() - timedelta(days=1)


#Python #Django #ORM #Database #Backend

━━━━━━━━━━━━━━━
By: @DataScience4
1