在Flask应用程序中导入错误

时间:2018-04-30 07:18:23

标签: python flask flask-sqlalchemy flask-restful

我正在构建一个烧瓶应用程序,并使用扩展名flask-marshmallow来序列化flask-alchemy查询输出。问题是我无法将marshmallow对象导入模型,我正在使用它。

__初始化__。PY

from flask import Flask
from flask_login import LoginManager
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow

# local imports
from config import app_config

db = SQLAlchemy()
login_manager = LoginManager()

def create_app(config_name):
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_object(app_config[config_name])
    app.config.from_pyfile('config.py')
    db.init_app(app)
    login_manager.init_app(app)
    login_manager.login_message = "You must be logged in to access this page."


    ## this is the object, I want to import to models
    ma = Marshmallow(app)
    ##
    migrate = Migrate(app, db)

    from app import models


    from .operations import operations as ops_blueprint
    app.register_blueprint(ops_blueprint)

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint)



    return app

我正在尝试在以下 models.py 文件中导入ma对象:

from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash

## here I'm importing ma, but getting the error mentioned in the last part of this question
from app import db, login_manager, ma
##
from sqlalchemy import func


class User(UserMixin, db.Model):
    """
    Creates a users table
    """
    __tablename__ = 'users'
    id = db.Column(db.BigInteger, primary_key=True)
    username = db.Column(db.String(60), unique=True)
    password_hash = db.Column(db.String(128))

    @property
    def password(self):
        raise AttributeError('password should not be readable')

    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)

    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)

    @login_manager.user_loader
    def load_user(user_id):
    return User.query.get(int(user_id)) 

获取以下错误消息:

Traceback (most recent call last):
  File "run.py", line 6, in <module>
    app = create_app(config_name)
  File "/Users/yousufsyed/Desktop/right/app/__init__.py", line 32, in create_app
    from app import models
  File "/Users/yousufsyed/Desktop/right/app/models.py", line 3, in <module>
    from app import ma, db, login_manager
ImportError: cannot import name ma

无法弄清楚如何在第二个文件中导入 ma ?有什么指针吗?

0 个答案:

没有答案