首页 > 其他 > 详细

Flask之 Marshmallow 踩坑实录

时间:2020-06-11 00:38:08      阅读:353      评论:0      收藏:0      [点我收藏+]

1.Marshmallow.ModelSchema 报错

  AttributeError: ‘Marshmallow‘ object has no attribute ‘ModelSchema‘

`from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
app = Flask(name)
app.config[‘SQLALCHEMY_DATABASE_URI‘] = ‘sqlite:///marshmallowjson.db‘

db = SQLAlchemy(app)
ma = Marshmallow(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))

class Item(db.Model):
id = db.Column(db.Integer, primary_key=True)
item_name = db.Column(db.String(50))
user_id = db.Column(db.Integer, db.ForeignKey(‘user.id‘))
user = db.relationship(‘User‘, backref=‘items‘)

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

class ItemSchema(ma.ModelSchema):
class Meta:
model = Item
@app.route(‘/‘)
def index():
users = User.query.all()
user_schema = UserSchema(many=True)
output = user_schema.dump(users).data
return jsonify({‘user‘: output})

if name == ‘main‘:
app.run(debug=True)
`

解决: pip install marshmallow-sqlalchemy

原因: 仅安装了烧瓶棉花糖,整个代码保持不变,但需要添加以使用SQLAlchemy。

Flask之 Marshmallow 踩坑实录

原文:https://www.cnblogs.com/coilan/p/13089131.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!