Django & Flask Topic
Database Integration with Flask-SQLAlchemy
-
What to learn: Connecting to a database, defining models as Python classes, and performing CRUD (Create, Read, Update, Delete) operations.
-
Django Connection: This is the single most important topic for Django. Flask-SQLAlchemy (an ORM) introduces you to the Object-Relational Mapper concept. Django has a built-in, very powerful ORM, and understanding the purpose of an ORM in Flask will make Django's ORM feel intuitive.
Database Integration with Flask-SQLAlchemy
Flask-SQLAlchemy is an ORM (Object Relational Mapper) that lets you interact with a database using Python classes instead of raw SQL.
This is the same concept Django’s ORM uses — but Flask's lightweight nature helps you understand it more transparently.
1. Installing Flask-SQLAlchemy
First install Flask and SQLAlchemy inside a virtual environment:
pip install flask flask_sqlalchemy
2. Connecting Flask to a Database
In Flask, you configure the database connection using app.config.
Example (SQLite database):
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///mydatabase.db' db = SQLAlchemy(app)
3. Defining Models as Python Classes
A model represents a table in the database.
A field is a column.
An object is a row.
Example User model:
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) def __repr__(self): return f"<User {self.username}>"
4. Creating the Database & Tables
python
Then in Python shell:
from app import db db.create_all()
This generates the mydatabase.db file with a users table.
5. CRUD Operations (Core of ORM Learning)
CREATE
user = User(username="john", email="john@example.com") db.session.add(user) db.session.commit()
READ
User.query.all() # list all users User.query.filter_by(id=1).first()
UPDATE
user = User.query.get(1) user.username = "john_doe" db.session.commit()
DELETE
user = User.query.get(1) db.session.delete(user) db.session.commit()
These simple operations teach the core ORM principles that directly transfer to Django.
Why This Topic is Critical for Django
Understanding Flask-SQLAlchemy makes Django’s ORM easier because both rely on the same concepts:
| Concept | Flask-SQLAlchemy | Django |
|---|---|---|
| Define a table | Python class | Python class |
| Define fields | db.Column() |
models.Field() |
| Create object | db.session.add() |
obj.save() |
| Read objects | User.query |
User.objects |
| Update / Save | commit session | .save() |
| Delete | .delete() + commit |
.delete() |
Examples:
Flask
User.query.filter_by(username="john").first()
Django
User.objects.filter(username="john").first()
Identical concept — different syntax.
Learning SQLAlchemy helps you understand:
-
model relationships
-
migrations
-
database schemas
-
how objects represent rows
This is EXACTLY what Django does behind the scenes, but more automatically.
Comments (0)
No comments yet. Be the first to comment!