Welcome! We're now moving from general Python and database theory to one of the most popular and powerful web frameworks in the ecosystem: Django. As a full-stack developer, this is where your Python skills and your database knowledge come together to build real applications.
This lesson covers the absolute fundamentals of "thinking in Django." We'll look at its core architecture (MVT), what makes it so powerful, how it compares to its main alternative (Flask), and its most famous feature: the Object-Relational Mapper (ORM).
Django follows a Model-View-Template (MVT) architecture. This is a design pattern that separates the application's logic into three distinct parts, which makes your code much cleaner, more organized, and easier to maintain.
It's very similar to the more common Model-View-Controller (MVC) pattern, but Django handles the "Controller" part differently.
Analogy: A Restaurant
Imagine you are at a restaurant:
In Django, the "Controller" part (the waiter who routes your order) is handled by the framework itself via the `urls.py` file.
Django is often called a "batteries-included" framework. This means it comes with almost everything you need to build a complex, secure, and scalable web application right out of the box.
This is the most common framework comparison. Both are excellent, but they have fundamentally different philosophies.
Analogy: Building a House
Summary:
- Choose Django for large, data-driven applications where you need an admin panel and a database out of the box (e.g., e-commerce, CMS, blogs).
- Choose Flask for small microservices, simple APIs, or projects where you want full control and flexibility.
The ORM (Object-Relational Mapper) is a powerful feature that lets you interact with your database using Python objects instead of writing raw SQL.
You define your database table as a Python `class` (the Model). The ORM is the translatorthat converts your Python commands into complex SQL queries.
Analogy: The Translator
Imagine you (a Python developer) need to talk to a database (a SQL-speaking accountant).
This is a practical follow-up. While the ORM is amazing, sometimes you need to break out of it and write raw SQL.
Here is a direct comparison of the two approaches, showing how to get the exact same result.
# First, assume we have this Django model:
#
# class User(models.Model):
# name = models.CharField(max_length=100)
# age = models.IntegerField()
# is_active = models.BooleanField(default=True)
# --------------------------------------------------
# GOAL: Get the names of all active users over 30
# --------------------------------------------------
# --- Method 1: The Django ORM ---
# This is Pythonic, clean, and 100% secure
# from SQL injection.
users = User.objects.filter(
age__gt=30,
is_active=True
).values_list('name', flat=True)
# The ORM translates this into a SQL query
# that looks something like this:
# "SELECT name FROM app_user WHERE age > 30 AND is_active = 1"
# --- Method 2: Raw SQL ---
# We write the SQL by hand.
# Note: We MUST parameterize to prevent SQL injection.
from django.db import connection
query = """
SELECT name FROM app_user
WHERE age > %s AND is_active = %s
"""
params = [30, True]
with connection.cursor() as cursor:
cursor.execute(query, params)
# .fetchall() returns a list of tuples, e.g., [('Alice',), ('Bob',)]
rows = cursor.fetchall()
# We have to manually flatten the list
users_raw = [row[0] for row in rows]
| Feature | Django ORM | Raw SQL |
|---|---|---|
| Usage | Use for 95% of queries. | Use for highly complex queries, or when performance is critical. |
| Security | Secure by default. | You are responsible for security (parameterization). |
| Readability | Excellent. It's just Python. | Can be complex. Mixes SQL strings in Python code. |
| Portability | Works on PostgreSQL, MySQL, SQLite, etc. | Query may be database-specific (e.g., using `LIMIT` vs. `TOP 1`). |
| Performance | Very good, but can be non-optimal for complex `JOIN`s. | Can be faster if you are a SQL expert and can hand-tune the query. |
3 questions · no sign-up, nothing stored
Progress is stored in this browser only - no sign-up, nothing sent anywhere.