【问题标题】:How do to pagination in Sanic?如何在 Sanic 中进行分页?
【发布时间】:2021-06-25 15:13:40
【问题描述】:

我试图获取请求参数并想在 sanic 中使用分页。是sanic电机。已使用 mongodb 查找详细信息。我想使用限制和跳过。

@app.route("/query", methods=["GET"])
async def query_params(request, count=10, page=1):
    a = request.args
    count = request.args['count'][0]
    page = a.get('page')
    students = await Student.find(as_raw=True).skip((page-1) * count).limit(count)
    return json_response(students.objects)

它给出错误 AttributeError: 'coroutine' object has no attribute 'skip' while processing path /query 同样的限制我得到这个错误

【问题讨论】:

    标签: pagination sanic


    【解决方案1】:

    这与 Sanic 无关。我认为问题在于您的 await 被应用于整个链条,而不是您的想法。如果你使用一些括号,你可以控制它如下。

    注意:我不熟悉 ORM,所以我不确定是否跳过和限制也需要等待。

    students = await (Student.find(as_raw=True)).skip((page-1) * count).limit(count)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-23
      • 2019-08-17
      • 2017-10-03
      • 2017-01-20
      • 2012-06-04
      • 2014-05-08
      • 2019-08-22
      • 2021-03-16
      相关资源
      最近更新 更多