0

I want to show only 5 results and next 5 in other page. my view.py

class QuestionList(APIView):

    def get(self, request, *args, **kwargs):
        res = Question.objects.all()

        paginator = Paginator(res, 5)
        serializer = QuestionSerializers(res, many=True)
        return Response({"Section": serializer.data})

how will my paginator work here ?

Muhammad Hassan
  • 14,086
  • 7
  • 32
  • 54
code_freak
  • 45
  • 8

1 Answers1

1

Django-rest provide its own method of pagination. You should use that for instead of building your own. You can find its docs here.

You just have to add following settings in your settings.

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
    'PAGE_SIZE': 100
}

Data in ListApiView will be paginated with this. In your current view

class QuestionList(APIView):

    def get(self, request, *args, **kwargs):
        res = Question.objects.all()

        page = self.paginate_queryset(res)
        serialized = QuestionSerializers(page, many=True)
        return self.get_paginated_response(serialized.data)
Muhammad Hassan
  • 14,086
  • 7
  • 32
  • 54