DRF get方法返回信息自定义

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)
        ]))

 

posted @ 2020-04-15 16:34  青春叛逆者  阅读(749)  评论(0编辑  收藏  举报