0

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.

cjukjones
  • 136
  • 1
  • 6

1 Answers1

3

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.

Linovia
  • 19,812
  • 4
  • 47
  • 48
  • Thanks very much Linovia. This did the trick. I had assumed this was probably the case yesterday and implemented something that worked but didn't get me the visible controls in the browseable API. This was the key. Much appreciated! – cjukjones Apr 15 '16 at 17:37