EVE Python循环依赖

时间:2019-05-09 05:26:47

标签: python dependencies eve

我更喜欢将模型存储在单独的文件中。通常,我更喜欢将文件分开。 我对EVE的第一印象是非常积极的,但是现在我很难为更大的项目创建可管理的应用程序结构:
我的“ auth”类要求导入“ app”
这些模型需要其特定的“ auth”类
在“ settings.py”中,您需要模型来创建应用->依赖地狱
谁能给我一些建议或链接到好的模板?

EVE App/
├── models/
│   ├── user.py
│   ├── ...
│   
│  
├── run.py
├── settings.py
├── auth.py
│   
└── ...

1 个答案:

答案 0 :(得分:0)

您可以尝试类似的方法,它可能会起作用:

app / __ 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

run.py

instance = create_app()
instance.run()

app / Models / ...在每个模型中

from ..database import db

class Plop(db.model):

app / database.py

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy() # you can override model there ex : model=MyModel