Django rest framework: how to turn off/on pagination in ModelViewSet

前端 未结 2 1231
广开言路
广开言路 2021-02-15 07:40

I\'m using Django REST framework with djangorestframework-csv with default pagination settings, but when request is with format \"CSV\", there is no need in paginaion. Is possib

2条回答
  •  广开言路
    2021-02-15 08:04

    if you just want to turn on or off pagination by client dynamically,you can custom Pagination class as:

    class Pagination(PageNumberPagination):
        page_size_query_param = 'page_size'
        max_page_size = 100
    
        def get_page_size(self, request):
            if self.page_size_query_param:
                page_size = min(int(request.query_params.get(self.page_size_query_param, self.page_size)),
                            self.max_page_size)
                if page_size > 0:
                    return page_size
                elif page_size == 0:
                    return None
                else:
                    pass
            return self.page_size
    

    use this class as your pagination_class and you can turn off pagination by request url "http://www.example.com/some_object/?page_size=0", ?page_size=0 will disable pagination. if you just want disable pagination when request format is CVS,you can try what danilke said.

提交回复
热议问题