场景:
paginate进行分页查询,返回结果需要进行json序列化
解决方案:
第一:
pip flask_marshmallow pip marshmallow_sqlalchemy
第二:
from import Marshmallow from marshmallow_sqlalchemy import SQLAlchemyAutoSchema ma = Marshmallow() class User(db.Model): account = db.Column(db.String(11), primary_key=True) username = db.Column(db.String(25), nullable=False) password = db.Column(db.String(64), nullable=False) #新版本是SQLAlchemyAutoSchema,老版本Schema class UserSchema(ma.SQLAlchemyAutoSchema): class Meta: model=User load_instance=True
第三:
users= User.query.paginate(page=1, per_page=10, error_out=False) user_schema = UserSchema() data = user_schema.dump(users.items, many=True)
第四:
ma.init_app(app)