python - EVE python循环依赖
问题描述
我更喜欢将模型存储在单独的文件中。一般来说,我更喜欢文件的某种分离。我对 EVE 的第一印象是非常积极的,但现在我很难为一个更大的项目创建一个可管理的应用程序结构:
我的“auth”类需要导入“app”
模型需要它们特定的“auth”类
和“settings.py”你需要模型来创建应用程序 - > 依赖地狱
谁能给我一些建议或一个好的样板链接?
EVE App/
├── models/
│ ├── user.py
│ ├── ...
│
│
├── run.py
├── settings.py
├── auth.py
│
└── ...
解决方案
您可以尝试类似的方法,它可能会起作用:
应用程序/__init__.py
from eve import Eve
from flask import current_app, request
# database
from .database import db
# blueprints
from users import blueprint1
from todo import blueprint2
create_app()
def set_username_as_none(username):
resource = request.endpoint.split('|')[0]
return current_app.data.driver.db[resource].update(
{"user" : username},
{"$set": {"user": None}},
multi=True
)
app = Eve()
# SQL Possible Solution 1
# register sqlalchemy to this app
with app.app_context():
db.init_app(app)
Migrate(app, db)
if not database_exists(db.engine.url):
create_database(db.engine.url)
print('Database créée : ' + str(database_exists(db.engine.url)))
# SQL Possible Solution 2
db.init_app(current_app)
Migrate(current_app, db)
if not database_exists(db.engine.url):
create_database(db.engine.url)
print('Database créée : ' + str(database_exists(db.engine.url)))
# register the blueprint to the main Eve application
app.register_blueprint(blueprint1)
app.register_blueprint(blueprint2)
# bind the callback function so it is invoked at each user deletion
app.users_deleted += set_username_as_none
return app
运行.py
instance = create_app()
instance.run()
app/Models/...在每个模型中
from ..database import db
class Plop(db.model):
应用程序/数据库.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy() # you can override model there ex : model=MyModel
推荐阅读
- python-3.x - 将矩阵的每个元素乘以向量(或数组)
- tensorflow - 加载 tensorflow.keras 模型时如何加载损失函数?
- html - 是否可以从 for 循环中使用 Alpine.js 在 img 中的 src 中注入变量?
- swift - 将 SwiftUI 视图设置为始终保持一种尺寸
- php - PHP为不同的输入提供相同的输出
- javascript - 从 Electron ipcRenderer.invoke() 访问返回对象/值
- javascript - Node/Electron 中的暗模式检测
- excel - 我的 Excel 公式不起作用,我在 Excel 365 中使用了 SUMIF
- javascript - 如何正确地将管理员用户添加到数据库?
- nestjs - 在数组 TypeOrm 中查找