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
原文:https://www.cnblogs.com/coilan/p/13089131.html