Flask-SQLAlchemy导入/上下文问题 [英] Flask-SQLAlchemy import/context issue
问题描述
./ site.py
./apps/ members / __ init__.py
./apps/members/models.py
apps.members
是一个Flask Blueprint。
现在,为了创建模型类,我需要保留应用程序,类似于:
#apps.members.models $ b $ from flask从flaskext导入current_app
。 sqlalchemy import SQLAlchemy
db = SQLAlchemy(current_app)
class成员(db.Model):
#字段here
传递
但是,如果我尝试将该模型导入到我的Blueprint应用程序中,则会遇到可怕的 RuntimeError:working在请求上下文之外
。我怎样才能在这里正确地保持我的应用程序?相对导入可能工作,但他们是非常丑陋的,并有自己的上下文问题,例如:
from ... site import app
#ValueError:试图相对导入超出顶层包
$ b模块不需要用应用程序立即初始化$ b
#apps.members.models $ b $ from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Member(db.Model):
#fields here
pass
然后在应用程序设置中,您可以调用 init_app
:
$ b
#apps.application .py
从flask进口Flask
from apps.members.models进口数据库
$ b应用程序= Flask(__ name__)
#
以后的数据db.init_app (app)
这样可以避免循环导入。 $ b
这个p attern 不是需要你把所有的模型放在一个文件中。只需将 db
变量导入到每个模型模块中即可。
$ b
示例
pre $
#apps.shared.models $ b $ from flask_sqlalchemy import SQLAlchemy
$ b $ db = SQLAlchemy()
$ c
$ b $ p $ #apps.members。模型
从apps.shared.models进口分贝
类成员(db.Model):
#TODO:实现这一点。
pass
#apps.reporting.members $ b $ from flask import render_template
from apps.members.models import Member
def report_on_members():
#TODO:实际上使用参数
members = Member.filter(1 == 1).all()
return render_template(report.html,members = members)
#apps.reporting 。从烧瓶导入路由
从apps.reporting.members导入蓝图
导入report_on_members
$ b报告= Blueprint(reporting,__name__)
报告。 route(/ member-report,methods = [GET,POST])(report_on_members)
#apps.application $ b $ from flask导入Flask $ b $ from apps.shared import db
from apps.reporting.routes导入报告
$ b app = Flask(__ name__)
db.init_app(app)
app.register_blueprint(报告)
注意:这是一个素描,它给了你一些权力 - 显然你可以做更多的事情使开发更容易(使用 create_app
模式,在特定文件夹中自动注册蓝图,等等。)
I want to structure my Flask app something like:
./site.py
./apps/members/__init__.py
./apps/members/models.py
apps.members
is a Flask Blueprint.
Now, in order to create the model classes I need to have a hold of the app, something like:
# 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 and import that model into my Blueprint app, I get the dreaded RuntimeError: working outside of request context
. How can I get a hold of my app correctly here? Relative imports might work but they're pretty ugly and have their own context issues, e.g:
from ...site import app
# ValueError: Attempted relative import beyond toplevel package
解决方案 The flask_sqlalchemy
module does not have to be initialized with the app 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 setup 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)
This way you can avoid cyclical imports.
This pattern does not necessitate the you place all of your models in one file. Simply 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 power this gives you - there is obviously quite a bit more that you can do to make development even easier (using a create_app
pattern, auto-registering blueprints in certain folders, etc.)
这篇关于Flask-SQLAlchemy导入/上下文问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!