Import / checkbox context-SQLAlchemy

I want to structure my Flask application like this:

./site.py ./apps/members/__init__.py ./apps/members/models.py 

apps.members is a flask project.

Now, to create model classes, I need to hold the application, for example:

 # apps.members.models from flask import current_app from flaskext.sqlalchemy import SQLAlchemy db = SQLAlchemy(current_app) class Member(db.Model): # fields here pass 

But if I try to import this model into my Blueprint application, I get the scary RuntimeError: working outside of request context . How can I grab my app correctly? Relative imports may work, but they are pretty ugly and have their own contextual issues, for example:

 from ...site import app # ValueError: Attempted relative import beyond toplevel package 
+104
python flask flask-sqlalchemy
Mar 13 2018-12-12T00:
source share
2 answers

The flask_sqlalchemy module flask_sqlalchemy not have to be initialized by the application right away - you can do this instead:

 # apps.members.models from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class Member(db.Model): # fields here pass 

And then in your application settings you can call init_app :

 # apps.application.py from flask import Flask from apps.members.models import db app = Flask(__name__) # later on db.init_app(app) 

In this way, cyclic imports can be avoided.

This template does not require placing all your models in one file. Just import the db variable into each of your model modules.

Example

 # apps.shared.models from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() 

 # apps.members.models from apps.shared.models import db class Member(db.Model): # TODO: Implement this. pass 

 # apps.reporting.members from flask import render_template from apps.members.models import Member def report_on_members(): # TODO: Actually use arguments members = Member.filter(1==1).all() return render_template("report.html", members=members) 

 # apps.reporting.routes from flask import Blueprint from apps.reporting.members import report_on_members reporting = Blueprint("reporting", __name__) reporting.route("/member-report", methods=["GET","POST"])(report_on_members) 

 # apps.application from flask import Flask from apps.shared import db from apps.reporting.routes import reporting app = Flask(__name__) db.init_app(app) app.register_blueprint(reporting) 

Note. this is a sketch of some of the features this gives you - obviously, you can do a little more to make development even easier (using the create_app template, autorecording drawings in specific folders, etc.)

+253
Mar 14 '12 at 2:15
source share

original app.py : https://flask-sqlalchemy.palletsprojects.com/en/2.x/quickstart/

 ... app = flask.Flask(__name__) app.config['DEBUG'] = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' db = flask.ext.sqlalchemy.SQLAlchemy(app) class Person(db.Model): id = db.Column(db.Integer, primary_key=True) ... class Computer(db.Model): id = db.Column(db.Integer, primary_key=True) ... # Create the database tables. db.create_all() ... # start the flask loop app.run() 

I just split one app.py into app.py and model.py without using Blueprint. In this case, the above answer does not work. The line code is necessary for work.

Before :

 db.init_app(app) 

after :

 db.app = app db.init_app(app) 

And the following link is very useful.

http://piotr.banaszkiewicz.org/blog/2012/06/29/flask-sqlalchemy-init_app/

+22
Nov 08 '13 at 0:15
source share



All Articles