如何使用list_route方法中的分页? 我有看法:
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)
答案 0 :(得分:17)
您需要对查询集进行分页
@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)
参考:http://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing