Active Record-style convenience methods (Model.query, save, delete) for SQLModel with Flask-SQLAlchemy.
Project description
sqlmodel-activerecord-mixin
Active Record–style convenience methods for SQLModel when using Flask-SQLAlchemy: Model.query, .save(), and .delete().
Install
pip install sqlmodel-activerecord-mixin
Usage
- Create a Flask app and init Flask-SQLAlchemy with SQLModel metadata (this package exposes the same
dbinstance):
from flask import Flask
from sqlmodel_activerecord import db
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///app.db"
db.init_app(app)
- Define models by inheriting from SQLModel, this mixin, and set
table=True:
from typing import Optional
from sqlmodel import SQLModel, Field
from sqlmodel_activerecord import ActiveRecordMixin
class Hero(SQLModel, ActiveRecordMixin, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
secret_name: str
- Use Active Record–style APIs (inside a Flask request or
app.app_context()):
# Create and save
hero = Hero(name="Iron Man", secret_name="Tony Stark")
hero.save()
# Query
Hero.query.all()
Hero.query.get(1)
Hero.query.filter_by(name="Iron Man").first()
Hero.query.count()
# Update (mutate then save)
hero.age = 50
hero.save()
# Delete
hero.delete()
Requirements
- Python >= 3.9
- Flask, Flask-SQLAlchemy, SQLModel
Development
pip install -e .
python usage_test.py # run module tests
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlmodel_activerecord_mixin-0.1.0.tar.gz.
File metadata
- Download URL: sqlmodel_activerecord_mixin-0.1.0.tar.gz
- Upload date:
- Size: 2.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0598040c27b391b7d3ab4a9077fb574520a082ce4f557b43fe4e95d62306bc97
|
|
| MD5 |
6bd904408a068a6b1c53ba6a06e92f21
|
|
| BLAKE2b-256 |
5f791982cf296f3bd1e596651b24dab8eafe912016817f65c721f84ac275f9a9
|
File details
Details for the file sqlmodel_activerecord_mixin-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sqlmodel_activerecord_mixin-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dda698d31f4594146336fbca00bdd49111b59bb14584f30b2d20f90687367b31
|
|
| MD5 |
1a6970c6c6dd63151757274d59e1ccf0
|
|
| BLAKE2b-256 |
3aff990ac125f5f84115cf3b72b88ea77d34afac8ecffb0177e3b0932dbcc12e
|