My application has a plan that needs to be modular and uses its own database models. I am using a factory application template.
Two problems that I often encounter with such a project structure are:
- Cyclic import.
- Inability to go through a
db object to drawings in a clean way so that they can create their own database models.
To avoid passing the db object around, now I started creating a new database object ( db = SQLAlchemy() ) inside the drawings themselves.
It works! It also avoids some problems with cyclic import, since this project now uses its own instance of db . My questions are: what problems will lead to this design? I am using gunicorn with a gevent working.
Here is a sample code showing how my models and factory are structured:
blueprints.status.models.py
from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class Status(db.Model): __tablename__ = 'states' id = db.Column(db.Integer, primary_key=True) job_id = db.Column(db.Integer) status = db.Column(db.String(120))
models.py
from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class Job(db.Model): __tablename__ = 'states' id = db.Column(db.Integer, primary_key=True) state = db.Column(db.String(120)
factory.py
from .blueprints.status.models import db as status_db
python flask design-patterns flask-sqlalchemy
Nitred Dec 24 '17 at 19:26 2017-12-24 19:26
source share