Pagination in django


Example code:


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

__all__ = ['DefaultPagination']

class DefaultPagination(PageNumberPagination):
page_size = 24
page_size_query_param = 'items_per_page'

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,
'items_per_page':self.page_size,
'results': data
})

def paginate_queryset(self,queryset, request, view=None):
if request.GET.get('pagination') == 'inactive':
self.page_size = queryset.count()
return super(DefaultPagination, self).paginate_queryset(queryset, request, view)



date:Nov. 14, 2023