如何使用 Django Rest Framework ReadOnlyModelViewSet 获取 "data" 而不是 "results"?
How to get "data" rather than "results" with Django Rest Framework ReadOnlyModelViewSet?
我已经阅读了这个问题,但接受的答案对我不起作用,而且我还有一个情况没有涵盖。
我正在使用 Django Rest Framework (3.5.3) 来提供一个 API 端点。该终点基于
class FruitTestReadOnlyViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Fruit.objects.all()
serializer_class = FruitSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
从那里返回的数据很好,只是它具有 "results" 属性而不是 "data" 属性。
我上面提到的 建议你这样做
class FruitTestReadOnlyViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Fruit.objects.all()
serializer_class = FruitSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
renderer_classes = (JSONRenderer,)
parser_classes = (JSONParser,)
但这对我来说没有区别,事实上我已经有了全局设置,我相信它涵盖了像这样的渲染和解析器方面
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
'PAGE_SIZE': 10,
'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
'DEFAULT_PAGINATION_CLASS':
'rest_framework_json_api.pagination.PageNumberPagination',
'DEFAULT_PARSER_CLASSES': (
'rest_framework_json_api.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
),
'DEFAULT_RENDERER_CLASSES': (
'djangorestframework_camel_case.render.CamelCaseJSONRenderer',
'rest_framework_json_api.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
),
'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata',
}
所以我很想知道最近是否有人这样做过,他们是怎么做到的?
顺便说一句,我还有其他基于 ModelViewSet
的 API 端点,它们工作正常并且提供 "data" 属性而不是 "results" ...这是一个示例其中
class TreeViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows Tree to be CRUDed.
"""
queryset = Tree.objects.all()
serializer_class = TreeSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
def get_queryset(self):
return Tree.objects.filter(username=self.request.user)
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',
}
如果您只想将此应用到 class FruitTestReadOnlyViewSet
,而不更改 settings.py 并为此设置 pagination_class class:
class FruitTestReadOnlyViewSet(viewsets.ReadOnlyModelViewSet):
pagination_class = Pagination
将默认分页的 get_paginated_response
方法从 ('results', data)
更改为 ('data', data)
即可。
我已经阅读了这个问题
我正在使用 Django Rest Framework (3.5.3) 来提供一个 API 端点。该终点基于
class FruitTestReadOnlyViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Fruit.objects.all()
serializer_class = FruitSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
从那里返回的数据很好,只是它具有 "results" 属性而不是 "data" 属性。
我上面提到的
class FruitTestReadOnlyViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Fruit.objects.all()
serializer_class = FruitSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
renderer_classes = (JSONRenderer,)
parser_classes = (JSONParser,)
但这对我来说没有区别,事实上我已经有了全局设置,我相信它涵盖了像这样的渲染和解析器方面
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
'PAGE_SIZE': 10,
'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
'DEFAULT_PAGINATION_CLASS':
'rest_framework_json_api.pagination.PageNumberPagination',
'DEFAULT_PARSER_CLASSES': (
'rest_framework_json_api.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
),
'DEFAULT_RENDERER_CLASSES': (
'djangorestframework_camel_case.render.CamelCaseJSONRenderer',
'rest_framework_json_api.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
),
'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata',
}
所以我很想知道最近是否有人这样做过,他们是怎么做到的?
顺便说一句,我还有其他基于 ModelViewSet
的 API 端点,它们工作正常并且提供 "data" 属性而不是 "results" ...这是一个示例其中
class TreeViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows Tree to be CRUDed.
"""
queryset = Tree.objects.all()
serializer_class = TreeSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
def get_queryset(self):
return Tree.objects.filter(username=self.request.user)
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',
}
如果您只想将此应用到 class FruitTestReadOnlyViewSet
,而不更改 settings.py 并为此设置 pagination_class class:
class FruitTestReadOnlyViewSet(viewsets.ReadOnlyModelViewSet):
pagination_class = Pagination
将默认分页的 get_paginated_response
方法从 ('results', data)
更改为 ('data', data)
即可。