Created
April 24, 2016 01:45
-
-
Save dkarchmer/b239d21e5e82c51433f755885f32bdfa to your computer and use it in GitHub Desktop.
How to paginate a custom detail_route get on djangorestframework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ViewSets define the view behavior. | |
class FooViewSet(viewsets.ModelViewSet): | |
lookup_field = 'slug' | |
queryset = Foo.objects.all() | |
serializer_class = FooSerializer | |
def get_queryset(self): | |
""" | |
This view should return a list of all records | |
""" | |
return Foo.objects.all().order_by('name') | |
@detail_route(methods=['get']) | |
def bar(self, request, slug=None): | |
foo = self.get_object() | |
qs = foo.bars.all().order_by('date') | |
page = self.paginate_queryset(qs) | |
if page is not None: | |
serializer = BarSerializer(page, many=True) | |
return self.get_paginated_response(serializer.data) | |
serializer = BarSerializer(qs, many=True) | |
return Response(serializer.data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment