Django Rest Framework 3.2.3 pagination not working

2019-06-09 02:07发布

The docs say I should get pagination for free when subclassing a generic List or ListCreateAPIView but there is no sign of any pagination happening.

Here is what I have in settings...

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS':   
        'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 25,
    'MAX_PAGE_SIZE': 50,
    'TEST_REQUEST_DEFAULT_FORMAT': 'json',
    'TEST_REQUEST_RENDERER_CLASSES': (...
    ),
    'DEFAULT_FILTER_BACKENDS': (...
    )
}

My View:

class RequestList(generics.ListCreateAPIView):

    # set context for serializers
    def get_serializer_context(self, *args, **kwargs):
        context = {
            'request': self.request,
            'view': self,
            'format': self.format_kwarg,
            'request_type_id': 1}
    return context 

    request_type_code_model_map = {
        "S": Request.objects.filter(request_type_id=1, status='open'),
        ...}

    def get(self, request, request_type_code="S", format=None, *args,  **kwargs):

        queryset = self.request_type_code_model_map.get(
            request_type_code, "S")
        serializer_class = RequestSerializer
        serializer = serializer_class(
            instance=queryset, context=self.get_serializer_context(),
            many=True)

     return Response(serializer.data)

Any help would be much appreciated. I've tried creating a custom paginator class, setting various configuration options in settings, but nothing I do seems to make any attempt at paginating. Pagination is working for Users and Groups which are using ViewSets, but not any of my views using generics. Any ideas, a clue as to what I am missing, or a solution would be much appreciated.

1条回答
狗以群分
2楼-- · 2019-06-09 02:57

You get the pagination for free provided you don't override parts that call it. You can browse the list source to see how it's implemented and how you should write your get method.

查看更多
登录 后发表回答