python 导入与 flask-security 冲突

python import conflict with flask-security

我 运行 经常进入这个 "import conflict" 我试图找到最 Python 的方法来解决它。我的应用程序的 __init__ 在我的 user.models 文件中实例化了 db 和更多,db 被广泛引用。但是,要使用 Flask-Security,我需要初始化 user_datastore,这需要我的 dbUser 模型。这些文件不能相互导入。我需要 __init__ 方法中的 User,但我还需要 User 模型中的 db

我尝试了各种安排(除了将我的许多互连模型移动到 __init__ 文件中),但这是最简单的设置和回溯。 也关注了 Python import conflict 但我还没有完全理解它。

__init__.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flaskext.markdown import Markdown
from flask_uploads import UploadSet, configure_uploads, IMAGES
from flask_security import Security, SQLAlchemyUserDatastore, utils
from flask_mail import Mail
import private

app = Flask(__name__)
app.config.from_object('settings')
db = SQLAlchemy(app)
mail = Mail(app)

# migrations
migrate = Migrate(app, db)

# markdown
md = Markdown(app, extensions=['fenced_code', 'tables'])

# images
uploaded_images = UploadSet('images', IMAGES)
configure_uploads(app, uploaded_images)

# CONFLICT 
from user.models import User, Role    
# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)


from roster import views
from sentence import views
from blog import views
from user import views

@app.before_first_request
def before_first_request():

    # Create the Roles "admin" and "end-user" -- unless they already exist
    user_datastore.find_or_create_role(name='admin', description='Administrator')
    user_datastore.find_or_create_role(name='end-user', description='End user')

    # Create two Users for testing purposes -- unless they already exists.
    # In each case, use Flask-Security utility function to encrypt the password.
    encrypted_password = utils.encrypt_password(private.STARTING_ADMIN_PASS)
    if not user_datastore.get_user('example@gmail.com'):
        user_datastore.create_user(email='example@gmail.com', password=encrypted_password)

    db.session.commit()

    user_datastore.add_role_to_user('example@gmail.com', 'admin')
    db.session.commit()

user.models

# THE OTHER HALF OF THE CONFLICT
from my_app import db
from blog.models import Post
from sentence.models import Sentence
from roster.models import Roster
from datetime import datetime
import datetime
from flask_security import UserMixin, RoleMixin

# Helper table for a many-to-many relationship
roles_users = db.Table('roles_users',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

class User(db.Model, UserMixin):
    # general variables
    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(155))
    last_name = db.Column(db.String(155))
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    active = db.Column(db.Boolean())
    confirmed_at = db.Column(db.DateTime())

    # relations
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))
    posts = db.relationship('Post', backref='user', lazy='dynamic')
    sentences = db.relationship('Sentence', backref='user', lazy='dynamic')

    def __init__(self, name, email):
        # create a roster
        roster = Roster("default", self.email)
        db.session.add(roster)
        db.session.commit()

    def __repr__(self):
        return '<User %r>' % self.username

    # __str__ is required by Flask-Admin (not using?), so we can have human-readable values for the Role when editing a User.
    # If we were using Python 2.7, this would be __unicode__ instead.
    def __str__(self):
        return self.name

    # __hash__ is required to avoid the exception TypeError: unhashable type: 'Role' when saving a User
    def __hash__(self):
        return hash(self.name)

回溯

Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from my_app import app
  File "/home/ubuntu/workspace/my_app/__init__.py", line 9, in <module>
    from user.models import User, Role
  File "/home/ubuntu/workspace/my_app/user/models.py", line 1, in <module>
    from my_app import db
ImportError: cannot import name 'db'

我通常做的是用使用过的扩展创建一个 extension.py 并使用应用工厂来初始化它们

extensions.py

from flask_security import Security
from flask_sqlalchemy import SQLAlchemy

security = Security()
db = SQLAlchemy()

然后在__init__.py我做初始化

def register_extensions(app):
    db.init_app(app)
    security.init_app(app, ...)

因此您可以在其他 类 中导入 db、security 和其他扩展,并且没有导入错误。有关更多信息和解释,请查看有关 Application Factories

的 Flask 文档