Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I leverage builtin pagination for a list_route in the Django Rest Framework? [closed]

How I can use pagination from list_route method? I have view:

class view(mixins.CreateModelMixin,
                mixins.RetrieveModelMixin,  
                viewsets.GenericViewSet):
...
@list_route(methods=['get'])
def some_method(self, request, **kwargs):
    queryset = Model.objects.all()
    serializer = self.get_serializer(queryset, many=True)
    return Response(serializer.data)
like image 961
Nikitka Avatar asked Feb 04 '16 10:02

Nikitka


1 Answers

You need to paginate your queryset

@list_route(methods=['get'])
def some_method(self, request, **kwargs):
    queryset = Model.objects.all()
    page = self.paginate_queryset(queryset)
    if page is not None:
        serializer = self.get_serializer(page, many=True)
        return self.get_paginated_response(serializer.data)
    serializer = self.get_serializer(queryset, many=True)
    return Response(serializer.data)

Refer: http://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing

like image 119
Akram Parvez Avatar answered Sep 26 '22 07:09

Akram Parvez