Flask 数据库查询 单条数据 如何使用 schema 格式化输出数据
# 单条数据 schema.dump 方法 content = db.session.query(Content).filter(Content.id == id).first() content.content = str_unescape(content.content) schema = ContentSchema() data['content'] = schema.dump(content) # 列表数据 curd.model_to_dicts 方法 def content(): data = get_template_data() page = request.args.get('page', type=int, default=1) pagesize = request.args.get('pagesize', type=int, default=3) query = db.session.query(Content) query = query.filter(Content.enable == 1,Content.status == 1) lists = query.paginate(page=page, per_page=pagesize, error_out=False) count = lists.total data['items'] = curd.model_to_dicts(schema=ContentSchema, data=lists.items) data['count'] = count data['page'] = page data['pagesize'] = pagesize data['pages'] = math.ceil(count / pagesize) return render_template('index/content.html', **data)