class MyPagination(PageNumberPagination): page_size = 10 page_size_query_param = "page_size" max_page_size = 100 def get_paginated_response(self, data): return Response(OrderedDict([ ('count', self.page.paginator.count), ('next', self.get_next_link()), ('previous', self.get_previous_link()), ('results', data) ])) 这种方法是设置全局的,如果想要个性化设置可以使用继承
from rest_framework.pagination import PageNumberPagination from collections import OrderedDict from rest_framework.response import Response class MyPagination(PageNumberPagination): page_size = 10 page_size_query_param = "page_size" max_page_size = 100 class MyPaginationSpecial(MyPagination): def get_paginated_response(self, data): return Response(OrderedDict([ ('count', self.page.paginator.count), ('next', self.get_next_link()), ('previous', self.get_previous_link()), ('results', data) ]))