python在工厂中的应用,将Python Flask应用配置为使用“ create_app”工厂并在模型类中使用数据库...

该问题探讨了在不使用SQLAlchemy的情况下,如何在Python Flask应用中使用工厂模式配置数据库连接。当尝试在/models/user.py中访问数据库连接时遇到困难。解决方案包括通过在app/__init__.py中初始化db对象并将其导入到需要的地方,以便在create_app工厂函数之外使用数据库连接。
摘要由CSDN通过智能技术生成

I'm having trouble getting my app to start when using a create_app() function. I'm new to building apps to this way and from all of my research it seems that my approach is different because I'm using my own database wrapper and not SQLAlchemy—which makes it easy because db.init_app(app) can be used.

My question is: I can't seem to access my database connection in /models/user.py... how do I fix this so I can use the db connection in that file?

This is my folder structure for the app, followed by those files listed:

/api

/common

database.py

/models

user.py

/resources

user.py

app.py

run.py

Here are my files

#

# File: run.py

#

from api.app import create_app

app = create_app(debug=True)

app.run(

host=app.config['APP_HOST'],

port=app.config['APP_PORT'],

debug=app.config['APP_DEBUG_FLASK'],

ssl_context=app.config['APP_SSL_CONTEXT']

)

#

# File: app.py

#

from logging.config import dictConfig

from flask import Flask

from flask_restful import Api

from api.config import LocalConfig, LiveConfig

from api.extensions import bcrypt, cors, jwt

from api.resources.user import *

from api.common.database import Database

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值