Learn SQLAlchemy and Django ORM in Python. Understand models, database queries, relationships and how ORMs simplify backend development — explained simply, with real examples
Introduction
Here’s a question worth sitting with for a moment: if you had to manually write a new SQL query every single time your application needed to save, fetch, update, or delete a record, how much of your time would actually go toward building features — and how much would just go toward repeating the same handful of database instructions in slightly different forms?
For any application of real size, the answer is: far too much.
Imagine developing a student management system with hundreds of database operations. You need to create student records, update course information, retrieve payments, and manage enrolments. Writing SQL queries for every single one of these operations by hand quickly becomes repetitive, error-prone, and genuinely tedious — not because SQL is hard, but because doing the same basic patterns over and over invites small, careless mistakes.
This is the exact problem Object-Relational Mappers, commonly known as ORMs, were built to solve. They let developers interact with relational databases using Python objects instead of raw SQL strings. Two important ORMs you’ll run into constantly as a Python developer are SQLAlchemy and Django ORM.
What Is an ORM?
An Object-Relational Mapper connects programming objects to relational database tables.
Instead of writing SQL for every database operation, developers can define Python models and use them to create, retrieve, update, and delete records.
For example, a Python class called Student can represent a database table named students. When you create an instance of that class and save it, the ORM translates that into the correct SQL behind the scenes — you write Python, and the ORM handles the translation.
What Is SQLAlchemy?
SQLAlchemy is a Python SQL toolkit and ORM used to work with relational databases.
It is widely used in Python applications, including projects built with FastAPI.
SQLAlchemy provides database connection management, SQL expression capabilities, and object-relational mapping — giving developers a lot of flexibility, including the option to drop down closer to raw SQL when a specific situation calls for it.
What Is Django ORM?
Django ORM is the built-in database abstraction system provided by Django.
It allows developers to define database models as Python classes and interact with records using Python methods.
It is closely integrated with Django’s models, migrations, and application structure — meaning it was designed from the start to work smoothly as one connected system, rather than as a separate tool bolted on afterward.
Practical Example: Creating a Student Model in Django ORM
from django.db import models
class Student(models.Model):
full_name = models.CharField(max_length=150)
email = models.EmailField(unique=True)
course = models.CharField(max_length=100)
def __str__(self):
return self.full_nameTo retrieve students:
students = Student.objects.all()To create a student:
Student.objects.create(
full_name="Jane Wanjiku",
email="jane@example.com",
course="Python"
)To retrieve a student by email:
student = Student.objects.get(
email="jane@example.com"
)These operations allow developers to interact with database records through Python — notice there’s no visible SQL anywhere in this code, yet real database operations are happening underneath. That’s the entire value an ORM provides.
Practical Example: SQLAlchemy Model
SQLAlchemy uses a different model structure.
A simplified SQLAlchemy 2.x example:
from sqlalchemy import create_engine, String
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.orm import Session
class Base(DeclarativeBase):
pass
class Student(Base):
__tablename__ = "students"
id: Mapped[int] = mapped_column(
primary_key=True
)
full_name: Mapped[str] = mapped_column(
String(150)
)
email: Mapped[str] = mapped_column(
String(150),
unique=True
)
engine = create_engine(
"sqlite:///college.db"
)
Base.metadata.create_all(engine)
with Session(engine) as session:
student = Student(
full_name="Jane Wanjiku",
email="jane@example.com"
)
session.add(student)
session.commit()This example creates a basic database table and inserts a student record using SQLAlchemy. Note the slightly more explicit structure here — SQLAlchemy asks you to be a bit more deliberate about defining your engine and session, which is part of why it’s often favored for applications that need finer control.
SQLAlchemy vs Django ORM
Feature | SQLAlchemy | Django ORM |
|---|---|---|
| Main use | General-purpose Python database toolkit and ORM | Database integration within Django |
| Framework dependency | Can be used independently | Integrated with Django |
| Flexibility | Extensive SQL and ORM control | Convenient Django application integration |
| Common pairing | FastAPI and other Python applications | Django |
| Migrations | Commonly managed with Alembic | Built-in Django migrations |
Neither one is strictly “better” — the right choice usually comes down to which framework you’re building with, which is why many Python developers end up comfortable with both rather than picking a side permanently.
Why Should Python Developers Learn ORMs?
1. Faster development
You can perform many database operations without manually writing repetitive SQL.
2. Better code organization
Models help organize application data and database interactions, keeping your database structure clearly documented in code rather than scattered across separate SQL files.
3. Database flexibility
Many ORM systems support different relational databases, meaning the same model code can often work with SQLite during development and PostgreSQL in production, with minimal changes.
4. Relationship management
You can represent one-to-one, one-to-many, and many-to-many relationships cleanly, using Python syntax instead of manually written JOIN statements for every related query.
5. Maintainability
Well-structured models can make application code easier to maintain, especially as a project grows and more developers need to understand how the data is organized.
However, developers should still understand SQL. ORMs do not remove the need to understand database performance, indexes, and query optimization — in fact, understanding what SQL an ORM generates behind the scenes is often what separates a developer who can fix a slow, poorly performing query from one who’s simply stuck.
A Beginner-Friendly Way to Think About This
If SQL feels like writing detailed instructions by hand every time, think of an ORM as a smart assistant who already knows SQL fluently — you describe what you want in familiar Python terms (“give me all students,” “save this new record”), and the assistant writes the correct SQL on your behalf. You still need to understand roughly what that assistant is doing — otherwise, when something goes wrong or runs slowly, you won’t know how to investigate — but day-to-day, it saves enormous amounts of repetitive work.
Practical Projects for Students
- Build a student management system using Django ORM.
- Create an inventory API using SQLAlchemy.
- Develop a course registration application.
- Build a POS system with product and sales relationships.
- Create an e-commerce backend with orders and customers.
Working through at least one project in each ORM gives you a genuine feel for how they differ in practice — reading about the difference only gets you so far compared to actually building the same kind of feature both ways.
Learn Python Database Development at DAZU Training Institute
DAZU Training Institute encourages practical software development and programming skills — and ORMs aren’t taught as an abstract add-on to “real” database knowledge. Students learn SQL fundamentals first, so the ORM makes sense as a genuine time-saver rather than a confusing black box, and then build real projects using both Django ORM and SQLAlchemy depending on the framework involved.
For example, a student at DAZU might develop a FastAPI application using SQLAlchemy in one project, and a complete college management system using Django ORM in another — giving them practical comfort with both of the ORMs they’re most likely to encounter in real backend development jobs.
If the difference between writing raw SQL and using an ORM still feels a bit abstract after reading this, that’s completely normal — this becomes genuinely clear only once you’ve built the same feature both ways and can see directly how much repetitive work the ORM removes. At DAZU, learners get exactly that kind of hands-on comparison, with instructors available to explain not just how to use an ORM, but what it’s actually doing underneath — so you’re never just copying syntax you don’t understand.
These are useful skills for anyone interested in becoming a Python backend developer.
Explore courses: https://dazu.ac.ke/courses
Contact: +254 722 922 454
Frequently Asked Questions
1. Is SQLAlchemy only for FastAPI?
No. SQLAlchemy can be used independently and with several Python frameworks.
2. Does Django have its own ORM?
Yes. Django includes a built-in ORM for defining models and managing database operations.
3. Do I still need to learn SQL?
Yes. Understanding SQL helps you write efficient queries, understand generated database operations, and troubleshoot performance problems.
4. Which ORM should I learn first?
If you are learning Django, start with Django ORM. If you are developing general Python applications or using FastAPI, SQLAlchemy is an important option.
5. What is a database migration?
A migration records and applies changes to database structure, such as adding a column or creating a table.
6. Will learning an ORM make it harder to understand raw SQL later?
No — if anything, it works the other way around. Seeing the Python code next to the SQL it generates (most ORMs let you view this) is actually a helpful way to reinforce SQL concepts, rather than a shortcut that avoids learning them.
Conclusion
ORMs simplify database development by allowing Python developers to work with relational data using Python models and objects.
Understanding both SQLAlchemy and Django ORM gives you flexibility when building different types of backend applications — and it’s a far less intimidating topic once you’ve actually built something with both and seen the difference for yourself.
Develop practical Python database skills with DAZU Training Institute — beginner-friendly, hands-on, and built around real projects.
Visit: https://dazu.ac.ke/courses
Contact: +254 722 922 454
Related articles
programming/ coding FastAPI Python Framework: Learn to Build Modern and High-Performance REST APIs
programming/ coding Authentication and Authorization in Python: Secure User Access in Backend Applications
programming/ coding JWT and OAuth2 in Python: Learn Modern API Authentication and Token Security
programming/ coding Python API Security: How to Protect Backend Applications from Cyber Threats
programming/ coding Database Relationships in Python: Learn One-to-One, One-to-Many and Many-to-Many Relationships
programming/ coding Python Application Deployment: Learn How to Launch Django and FastAPI Projects Online
programming/ coding Python Programming Course in Kenya: Learn Functions, Data Structures and Practical Problem-Solving
programming/ coding Python Programming Course in Kenya: Build Real-World Projects with APIs, Databases and Automation
You might also like
security What is Cybersecurity? A Complete Guide for Beginners
Nov 16, 2026
software development Software Engineering Course in Thika: Build Real Skills, Real Projects, Real Career Opportunities
Nov 13, 2026
beauty el Nails, Acrylic Nails and Nail Art: Which Skills Should You Learn?
Nov 13, 2026
beauty Beauty Skills You Can Learn and Turn Into a Profitable Business in Kenya
Nov 13, 2026
beauty Manicure and Pedicure Course in Kenya: Everything You Need to Know
Nov 13, 2026
beauty : How to Become a Professional Nail Technician in Kenya
Nov 13, 2026