I have a Django Restframework project. Here is my view:
class JobView(viewsets.ReadOnlyModelViewSet):
queryset = Job.objects.all()
serializer_class = JobSerializer
filter_backends = [filters.OrderingFilter, DjangoFilterBackend, filters.SearchFilter]
search_fields = [...]
ordering_fields = ['jobId']
ordering = ['-jobId']
filterset_fields = ['jobId', 'status']
pagination_class = StandardResultsSetPagination
and my pagination class:
class StandardResultsSetPagination(LimitOffsetPagination):
page_size = 1
The view is registerd in a router:
router.register(r'jobs', JobView)
The problem is, it does not paginate at all. It just ignores the pagination_class attribute.
I also tried:
class JobView(generics.ListAPIView)
And registered the view without the router. But the problem is the same. It does not paginate and just returns a json list with all jobs.
How can achieve pagination in this view without defining it for the project in settings.py globally?
CodePudding user response:
Does your StandardResultsSetPagination
inherit from the correct pagination style? LimitOffsetPagination does not limit the output by default (aside from the PAGE_SIZE
setting) and requires query parameters to set the limit. It also does not have a page_size
class attribute, contrary to PageNumberPagination.
CodePudding user response:
Just try to replace your pagination class with the below code
from rest_framework.pagination import PageNumberPagination
class CustomPagination(PageNumberPagination):
page_size = 5
page_size_query_param = 'page_size'