小编dex*_*per的帖子

从 Flask 蓝图访问数据库

我正在使用 Flask 和 SQLAlchemy 构建 RESTful API 应用程序,并尝试应用蓝图模式来注册我的路线。蓝图 @route 函数包含对数据库会话对象的引用。我希望以“代理风格”访问它 - 您可以使用app.current_app访问 Flask.app ,但我的实现不起作用。

wsgi.py:(从这里运行)

# imports
from wallet.walletapp import main

app = create_app(url='sqlite:///./test.db')
dbase = Database(url='sqlite:///./test.db')

db = dbase.create_session()
models.Base.metadata.create_all(bind=dbase.engine)

abort = Abort(app, db)
app.config['database'] = db
app.config['abortion'] = abort
with app.app_context:
    app.register_blueprint(main)
app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)

钱包应用程序.py

main = Blueprint('main', __name__)
app = current_app
db = current_app.config['database']
abort = current_app.config['abortion']

# Get wallet list
@main.route('/v1/wallets', methods=['GET'])
def get_wallets():
    '''
    Get all the wallets and their balances
    '''
    wallets = …
Run Code Online (Sandbox Code Playgroud)

python sqlalchemy blueprint flask

3
推荐指数
1
解决办法
2121
查看次数

标签 统计

blueprint ×1

flask ×1

python ×1

sqlalchemy ×1