[Django]-Django api framework getting total pages available

39๐Ÿ‘

โœ…

You can create your own pagination serializer:

from django.conf import settings
from rest_framework import pagination
from rest_framework.response import Response


class YourPagination(pagination.PageNumberPagination):

    def get_paginated_response(self, data):
        return Response({
            'links': {
               'next': self.get_next_link(),
               'previous': self.get_previous_link()
            },
            'count': self.page.paginator.count,
            'total_pages': self.page.paginator.num_pages,
            'results': data
        })

In your configuration in settings.py you add YourPagination class as the Default Pagination Class.

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'my_project.apps.pagination.YourPagination',
    'PAGE_SIZE': 20
}

References:

๐Ÿ‘คEvans Murithi

5๐Ÿ‘

You can extend PageNumberPagination class and override get_paginated_response method to get total pages count.

class PageNumberPaginationWithCount(pagination.PageNumberPagination):
    def get_paginated_response(self, data):
        response = super(PageNumberPaginationWithCount, self).get_paginated_response(data)
        response.data['total_pages'] = self.page.paginator.num_pages
        return response

And then in settings.py, add PageNumberPaginationWithCount class as the Default Pagination Class.

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'my_project.apps.pagination.PageNumberPaginationWithCount',
    'PAGE_SIZE': 30
}
๐Ÿ‘คcodewithrashid

1๐Ÿ‘

yes, inside your view you can make a custom class pagination , and use it inside your CBV , and no need to add any extra codes inside your settings.py module , and here is a simple example :

from rest_framework.pagination import PageNumberPagination
from rest_framework.generics import ListAPIView
from rest_framework.response import Response


from .serilizers import App1PostSerializer
from .models import Post

class MyCustomPagination(PageNumberPagination):
    page_size = 2
    page_size_query_param = 'page_size'
    def get_paginated_response(self, data):
        return Response({
            'page_size': self.page_size,
            'total_objects': self.page.paginator.count,
            'total_pages': self.page.paginator.num_pages,
            'current_page_number': self.page.number,
            'next': self.get_next_link(),
            'previous': self.get_previous_link(),
            'results': data,
        })


class PostsView(ListAPIView):
    queryset = Post.objects.all()
    serializer_class = App1PostSerializer
    pagination_class = MyCustomPagination

output results:

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "page_size": 2,
    "total_objects": 3,
    "total_pages": 2,
    "current_page_number": 1,
    "next": "http://127.0.0.1:8000/api/posts/?page=2",
    "previous": null,
    "results": [
        {
            "id": 4,
            "post_user": 1,
            "post_title": "tt",
            "post_description": "ttt",
            "post_created_date": "2022-02-05T11:12:24.207985Z",
            "post_updated_date": "2022-02-05T11:12:24.207985Z",
            "created_by": "admin"
        },
        {
            "id": 5,
            "post_user": 1,
            "post_title": "tt",
            "post_description": "tt",
            "post_created_date": "2022-02-05T11:12:30.155031Z",
            "post_updated_date": "2022-02-05T11:12:30.155031Z",
            "created_by": "admin"
        }
    ]
}

this is just example , and change it based on your requirement / case .
i hope this helpful.

๐Ÿ‘คK.A

Leave a comment