分页不适用于 django 中的视图集 rest_framework
Pagination not working for viewsets in django rest_framework
paginate.py
class StandardResultsSetPagination(PageNumberPagination):
page_size = 3
page_size_query_param = 'page_size'
max_page_size = 5
Serializers.py
class AreaUsersSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'first_name', 'areas')
Views.py
class AreaUsers(viewsets.ViewSet):
queryset = User.objects.all()
serializer_class = AreaUsersSerializer
pagination_class = StandardResultsSetPagination # Not working
def list(self, request):
queryset = User.objects.all()[:10]
serializer = AreaUsersSerializer(queryset, many=True)
return Response(serializer.data)
我只是想知道这段代码有什么问题为什么分页不是 applying.It 在 api 视图中显示所有 10 个值
试试这个:
class StandardResultsSetPagination(pagination.PageNumberPagination):
def get_paginated_response(self, data):
return Response({
'links': {
'next': self.get_next_link(),
'previous': self.get_previous_link()
},
'current_page': int(self.request.query_params.get('page', 1)),
'total': self.page.paginator.count,
'per_page': self.page_size,
'total_pages': round(self.page.paginator.count/self.page_size, 1),
'data': data,
})
class AreaUsers(viewsets.ViewSet):
queryset = User.objects.all()
serializer_class = AreaUsersSerializer
def list(self, request):
queryset = User.objects.all()[:10]
paginator = StandardResultsSetPagination()
page_size = 5
paginator.page_size = page_size
result_page = paginator.paginate_queryset(queryset, request)
serializer = UserSerializer(result_page, many=True)
return paginator.get_paginated_response(serializer.data)
paginate.py
class StandardResultsSetPagination(PageNumberPagination):
page_size = 3
page_size_query_param = 'page_size'
max_page_size = 5
Serializers.py
class AreaUsersSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'first_name', 'areas')
Views.py
class AreaUsers(viewsets.ViewSet):
queryset = User.objects.all()
serializer_class = AreaUsersSerializer
pagination_class = StandardResultsSetPagination # Not working
def list(self, request):
queryset = User.objects.all()[:10]
serializer = AreaUsersSerializer(queryset, many=True)
return Response(serializer.data)
我只是想知道这段代码有什么问题为什么分页不是 applying.It 在 api 视图中显示所有 10 个值
试试这个:
class StandardResultsSetPagination(pagination.PageNumberPagination):
def get_paginated_response(self, data):
return Response({
'links': {
'next': self.get_next_link(),
'previous': self.get_previous_link()
},
'current_page': int(self.request.query_params.get('page', 1)),
'total': self.page.paginator.count,
'per_page': self.page_size,
'total_pages': round(self.page.paginator.count/self.page_size, 1),
'data': data,
})
class AreaUsers(viewsets.ViewSet):
queryset = User.objects.all()
serializer_class = AreaUsersSerializer
def list(self, request):
queryset = User.objects.all()[:10]
paginator = StandardResultsSetPagination()
page_size = 5
paginator.page_size = page_size
result_page = paginator.paginate_queryset(queryset, request)
serializer = UserSerializer(result_page, many=True)
return paginator.get_paginated_response(serializer.data)