Answers for "drf page number pagination explained"

1

drf default pagination

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
    'PAGE_SIZE': 100
}
Posted by: Guest on August-15-2021
0

how to use drf pagination directly

from rest_framework.views import APIView
from rest_framework.pagination import PageNumberPagination

class ProductList(APIView, PageNumberPagination):
    # number of items per page by default
    page_size = 1000
    # max number of items per page
    max_page_size = 1000

    def get_queryset(self):
        product_sync_ts = self.request.GET.get('product_sync_ts', None)
        if product_sync_ts:
            products = GrProduct.objects.filter(update_ts__gt=product_sync_ts)
            return self.paginate_queryset(products)

        raise APIException400(request, {'details': "Bad Request"})

    def get(self, request):
        products = self.get_queryset()
        serializer = SyncedProductSerializer(instance={'products': products})
        return self.get_paginated_response(serializer.data)
Posted by: Guest on January-29-2022

Browse Popular Code Answers by Language