Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Want to return current page limit in response in Django Rest Framework

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
        )

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

'current_items_per_page': len(self.page.object_list),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading