首页 > 解决方案 > EVE python循环依赖

问题描述

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

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

标签: pythondependencieseve

解决方案


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

应用程序/__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

推荐阅读