烧瓶棉花糖ImportError:无法导入名称字段

时间:2019-10-17 07:32:32

标签: python python-2.7 flask

几周前我开始学习烧瓶,然后按照烧瓶大型教程进行学习。现在,我想自己进行一些编程,我尝试使用flask-marshmallow从 json 格式从数据库返回数据,但被卡住了。我收到一条错误消息,提示 ImportError:无法导入名称字段

This is full error message

这是 models.py 模块:

followers = db.Table('followers',
db.Column('follower_id', db.Integer, db.ForeignKey('user.id')),
db.Column('followed_id', db.Integer, db.ForeignKey('user.id')))


class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
country = db.Column(db.String(140))
nationality = db.Column(db.String(140))
password_hash = db.Column(db.String(128))
# Definisanje veze sa Post tabelom
posts = db.relationship('Post', backref='author', lazy='dynamic')
about_me = db.Column(db.String(140))
last_seen = db.Column(db.DateTime, default=datetime.utcnow())
# Definisanje veze sa followers tabelom
followed = db.relationship(
    'User', secondary=followers,
    primaryjoin=(followers.c.follower_id == id),
    secondaryjoin=(followers.c.followed_id == id),
    backref=db.backref('followers', lazy='dynamic'), lazy='dynamic')

class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.String(140))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

def __repr__(self):
    return '<Post {}>'.format(self.body)

#Declaring marshmallow ModelSchema
    class UserSchema(ma.ModelSchema):
    class Meta:
        model = User

这是 routes.py 模块:

@app.route('/all_users', methods=['GET'])
def get_all_users():
    users = User.query.all()
    user_schema = UserSchema(many=True)
    out = user_schema.dump(users).data
    return jsonify(out)

我没有提供所有代码,但如有必要,我会提供它。

由于错误消息将我指向microblog.py和 init .py,这也是我的应用程序中的模块,因此我将包括这两个模块。

microblog.py

from app import app, db
from app.models import User, Post


@app.shell_context_processor
def make_shell_context():
    return {'db': db, 'User': User, 'Post': Post}

__ init __。py

from flask import Flask
from logging.handlers import RotatingFileHandler
import logging
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_mail import Mail
from flask_bootstrap import Bootstrap
from flask_moment import Moment
import os


'''Inicijalizacija ekstenzija'''
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = 'login'
mail = Mail(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
ma = Marshmallow(app)

from app import routes, models, errors


if not app.debug:

    if not os.path.exists('logs'):
        os.mkdir('logs')
    file_handler = RotatingFileHandler('logs/microblog.log', maxBytes=10240,
                                       backupCount=10)
    file_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
    file_handler.setLevel(logging.INFO)
    app.logger.addHandler(file_handler)

    app.logger.setLevel(logging.INFO)
    app.logger.info('Microblog startup')

0 个答案:

没有答案