[Django]-How to get "data" rather than "results" with Django Rest Framework ReadOnlyModelViewSet?

5👍

pagination.py

from collections import OrderedDict
from rest_framework.response import Response

class Pagination(PageNumberPagination):

    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('count', self.page.paginator.count),
            ('next', self.get_next_link()),
            ('previous', self.get_previous_link()),
            ('data', data)
        ]))

settings.py

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS':
        'path_to_pagination.pagination.Pagination',

}

if you only want apply this to class FruitTestReadOnlyViewSet,not change settings.py and set pagination_class for this class:

class FruitTestReadOnlyViewSet(viewsets.ReadOnlyModelViewSet):
    pagination_class = Pagination

change default pagination’s get_paginated_response method from ('results', data) to ('data', data) will fine.

👤Ykh

Leave a comment