Flask integration for sqlorm
Install:
$ pip install flask-sqlorm
Setup:
from flask import Flask
from flask_sqlorm import FlaskSQLORM
app = Flask()
db = FlaskSQLORM(app, "sqlite://:memory:")
All exports from the sqlorm
package are available from the extension instance.
Define some models:
class Task(db.Model):
id: db.PrimaryKey[int]
title: str
done: bool = db.Column(default=False)
Start a transaction using the db object:
with db:
Task.create(title="first task")
In views, the current session is available using db.session
Configure the sqlorm engine using the extension's constructor or init_app()
. Configuration of the engine is performed using the URI method.
Additional engine parameters can be provided as keyword arguments.
Configuration can also be provided via the app config under the SQLORM_
namespace. Use SQLORM_URI
to define the database URI.
Model classes have the additional methods:
find_one_or_404
: same asfind_one
but throw a 404 when no results are returnedget_or_404
: same asget
but throw a 404 when no results are returned
Some CLI commands are available under the db command group. Check out flask db --help
for a list of subcommands.