从Django REST Framework获取不合理的结果

时间:2021-02-16 20:27:46

I have pagination enabled by default, which is based on PageNumberPagination; this had sufficed until now as the API was only used by a front-end. Now we try to build automation on top of it, and I’d like to pass the full, unpaginated result set back to the client.

我默认启用了分页,它基于PageNumberPagination;到目前为止,这已经足够了,因为API仅用于前端。现在我们尝试在它之上构建自动化,并且我想将完整的,未涂抹的结果集传递回客户端。

Is there a way to disable pagination for specific requests, e.g. if a request parameter is passed?

有没有办法禁用特定请求的分页,例如如果传递了请求参数?

2 个解决方案

#1


0  

You can approximate this with a request limit set to an unfeasibly large number, but you need to configure Django first:

您可以通过将请求限制设置为不可行的大数来对此进行近似,但您需要先配置Django:

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination'
}

Then make a ridiculously high limit request:

然后做出一个可笑的高限制请求:

GET https://api.example.org/accounts/?limit=999999999999

LimitOffsetPagination - django-rest-framework.org

LimitOffsetPagination - django-rest-framework.org

#2


0  

I actually did go with a custom pagination class:

我确实选择了自定义分页类:

class Unpaginatable(PageNumberPagination):
    def paginate_queryset(self, queryset, request, view=None):
        if getattr(request, 'get_all', False):
            return None

        return super(BuildListPagination, self).paginate_queryset(queryset, request, view=view)

Now I just have to set request.get_all = True in my viewset and I get all the items.

现在我只需要在我的视图集中设置request.get_all = True,我就可以获得所有项目。

#1


0  

You can approximate this with a request limit set to an unfeasibly large number, but you need to configure Django first:

您可以通过将请求限制设置为不可行的大数来对此进行近似,但您需要先配置Django:

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination'
}

Then make a ridiculously high limit request:

然后做出一个可笑的高限制请求:

GET https://api.example.org/accounts/?limit=999999999999

LimitOffsetPagination - django-rest-framework.org

LimitOffsetPagination - django-rest-framework.org

#2


0  

I actually did go with a custom pagination class:

我确实选择了自定义分页类:

class Unpaginatable(PageNumberPagination):
    def paginate_queryset(self, queryset, request, view=None):
        if getattr(request, 'get_all', False):
            return None

        return super(BuildListPagination, self).paginate_queryset(queryset, request, view=view)

Now I just have to set request.get_all = True in my viewset and I get all the items.

现在我只需要在我的视图集中设置request.get_all = True,我就可以获得所有项目。