我正在使用Miguel Grinberg的Flask REST API repo,但我没有返回JSON分页结果。这些示例在线使用html模板,但我只想返回一些结果(20),并最终返回上一页和下一页的链接。当我在这句话之后立即返回代码时,我得到“分页对象不可迭代”:
/dashboard
我知道我传递了错误的对象,但我不确定是否应该使用其他模块,或者我是否在正确的道路上。有没有人有建议达到我的最终目标?
Miguel回购中的原始代码是:
def get_customers():
return jsonify({'customers': [customer.get_url() for customer in
Customer.query.paginate(page=1, per_page=1)]})
我正在使用的相关代码:
@app.route('/customers/', methods=['GET'])
def get_customers():
return jsonify({'customers': [customer.get_url() for customer in
Customer.query.all()]})
答案 0 :(得分:6)
请参阅the API docs。
如果要迭代Pagination对象,请使用(例如)
Customer.query.paginate(page=1, per_page=1).items
是该页面的项目集合。