Reputation: 3072
How to return pagination with django framework?
I'm trying to use the class LimitOffsetPagination.
Where am I going wrong?
Thank you guys
class Sellers(APIView):
pagination_class = LimitOffsetPagination
def get(self, request):
transactions = Transactions.objects.all()
page = self.paginate_queryset(transactions, request)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(transactions, many=True)
return Response(serializer.data)
page = self.paginate_queryset(transactions, request)
AttributeError: 'Sellers' object has no attribute 'paginate_queryset'
Upvotes: 1
Views: 1444
Reputation: 51938
I don't think APIView has pagination
support. You need to use GenericAPIView
:
class Sellers(GenericAPIView):
pagination_class = LimitOffsetPagination
def get(self, request):
transactions = Transactions.objects.all()
page = self.paginate_queryset(transactions)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = YourSerializer(transactions, many=True)
return Response(serializer.data)
Or more simply use ListAPIView
where django rest framework handles everything for you:
class Sellers(ListAPIView):
serializer_class = YourSerializer # you need to define a serializer and put it here
queryset = Transactions.objects.all()
def dispatch(self, request, *args, **kwargs):
if request.GET.get('offset', None) and self.request.GET.get('limit', None):
self._paginator = LimitOffsetPagination
else:
self._paginator = PageNumberPagination
return super(Sellers, self).dispatch(request, *args, **kwargs)
def get_queryset(self):
return Transactions.objects.filter(user=self.request.session['user_id'])
# You don't need to override the `.get()` method
Upvotes: 2
Reputation: 2905
Editing Ruddura's second answer, using self.pagination_class
instead of self._paginator
because it throws some strange errors.
Edited answer:
class Sellers(ListAPIView):
serializer_class = YourSerializer # you need to define a serializer and put it here
queryset = Transactions.objects.all()
def dispatch(self, request, *args, **kwargs):
if request.GET.get('offset', None) and self.request.GET.get('limit', None):
self.pagination_class = LimitOffsetPagination
else:
self.pagination_class = PageNumberPagination
return super(Sellers, self).dispatch(request, *args, **kwargs)
def get_queryset(self):
return Transactions.objects.filter(user=request.session['user_id'])
# ...
return self.get_paginated_response(self.paginate_queryset(serializer.data))
AND in settings.py add:
REST_FRAMEWORK = {
# ...
'PAGE_SIZE': 100 //Change to preference
# ...
}
Upvotes: 0
Reputation: 11665
We can take advantage of generic ListAPIView
. You can use built-in functionality to speedup the development like below.
class Sellers(ListAPIView):
pagination_class = LimitOffsetPagination
queryset = Transactions.objects.all()
Reference: https://www.django-rest-framework.org/api-guide/generic-views/#listapiview
Upvotes: 0