Pagination on viewset list()

2,640 views
Skip to first unread message

Bayard Randel

unread,
May 29, 2014, 6:25:20 PM5/29/14
to django-res...@googlegroups.com
Hi there,

I'm using ViewSets exclusively and have an issue where pagination does not appear to be preserved when overriding list(). My goal is ideally to use differing serializers for list() and detail().

My ViewSet currently looks like this:

    class MyViewSet(viewsets.ReadOnlyModelViewSet):
        
        queryset = MyModel.objects.all()
        serializer_class = PresentationSerializer
        filter_backends = (filters.SearchFilter,)
        search_fields = ['name', 'keywords', 'description']
        max_paginate_by = 200

However, if I override list() I seem to unfortunately lose the pagination provide by the ModelViewSet

e.g.

    class MyViewSet(viewsets.ReadOnlyModelViewSet):
        
        model = MyModel
  
        def list(self, request):
                  queryset = MyModel.objects.all()
        filter_backends = (filters.SearchFilter,)
        search_fields = ['name', 'keywords', 'description']
                  serializer = MyModelListSerializer(queryset, many=True)
                  return Response(serilaizer.data)


Clearly I'm doing something wrong. Any help would be greatly appreciated, thanks!

Anoop Malav

unread,
Jun 6, 2014, 12:54:21 AM6/6/14
to django-res...@googlegroups.com


On Friday, May 30, 2014 3:55:20 AM UTC+5:30, Bayard Randel wrote:
Hi there,

I'm using ViewSets exclusively and have an issue where pagination does not appear to be preserved when overriding list().
 
My goal is ideally to use differing serializers for list() and detail().

 
My ViewSet currently looks like this:

    class MyViewSet(viewsets.ReadOnlyModelViewSet):
        
        queryset = MyModel.objects.all()
        serializer_class = PresentationSerializer
        filter_backends = (filters.SearchFilter,)
        search_fields = ['name', 'keywords', 'description']
        max_paginate_by = 200

However, if I override list() I seem to unfortunately lose the pagination provide by the ModelViewSet

e.g.

    class MyViewSet(viewsets.ReadOnlyModelViewSet):
        
        model = MyModel
  
        def list(self, request):
                  queryset = MyModel.objects.all()
        filter_backends = (filters.SearchFilter,)
        search_fields = ['name', 'keywords', 'description']
                  serializer = MyModelListSerializer(queryset, many=True)
                  return Response(serilaizer.data)

You are simply returning dict, not paginated data.

Clearly I'm doing something wrong. Any help would be greatly appreciated, thanks!

 Hope it'll work.

Bayard Randel

unread,
Jun 8, 2014, 6:32:26 PM6/8/14
to django-res...@googlegroups.com
Thanks very much Anoop, I wasn't aware of the DetailSerializerMixin in DRF extensions! The feels like an important omission from the DRF viewset implementation - any chance that this will be merged into DRF core?


Tom Christie

unread,
Jun 11, 2014, 11:53:17 AM6/11/14
to django-res...@googlegroups.com
any chance that this will be merged into DRF core?

Nope, not currently something that I'd like to see in core, sorry.
If you search in the issues, you may be able to find the original discussion around this being in core or in a third party package. :)
All the best,

  Tom
Reply all
Reply to author
Forward
0 new messages