Home > Back-end >  Want to return current page limit in response in Django Rest Framework
Want to return current page limit in response in Django Rest Framework

Time:09-17

Want to return current page limit current_items_per_page in response.

  • pagination.py
class CustomPageNumberPagination(pagination.PageNumberPagination):
    page_size = 10 # Number of objects to return in one page
    page_size_query_param = 'total_count'
    max_page_size = 20
    page_query_param = 'page_no'
    page_size_query_param = 'limit_pr_page'

    def generate_response(self, query_set, serializer_obj, request):

        Try:
            page_data = self.paginate_queryset(query_set, request)

        except NotFoundError as e:
            return Response({"response": False, "return_code": "NotFoundError", "result": {}, "message": errors["invalid_page"]},  status=status.HTTP_400_BAD_REQUEST)

        serialized_page = serializer_obj(page_data, many=True)
        return Response({
        "response": True,
        "return_code": "success",
        "result":serialized_page.data,
        "message": "Success",
        'page_no': self.page.number,
        'current_items_per_page': self."?",
        'max_limit_per_page': self.max_page_size,
        'last_page': self.page.paginator.num_pages,
        'total_items': self.page.paginator.count,
        },
        status=status.HTTP_200_OK
        )

CodePudding user response:

You can use len() of current page's object_list:

'current_items_per_page': len(self.page.object_list),
  • Related