如何在 Django Rest Framework JSON API 中生成 JSON-API 数据属性与结果属性?
How to generate JSON-API data attribute vs results attribute in Django Rest Framework JSON API?
我有一个使用 Django Rest Framework 的 django 1.9.2 项目 JSON API:
https://github.com/django-json-api/django-rest-framework-json-api:
我的视图集如下所示:
class QuestionViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows questions and answers to be read.
"""
resource_name = 'questions'
queryset = Question.objects.all()
serializer_class = QuestionSerializer
renderers = renderers.JSONRenderer
parsers = parsers.JSONParser
典型的响应如下所示:
{"links": {"first": "http://testserver/api/v1/coaches?page=1", "last": "http://testserver/api/v1/coaches?page=1", "next": null, "prev": null}, "results": [{"id": 1, "created": "2016-02-11T02:41:22.569000Z", "updated": null, "deleted": null, "uuid": "0473c709-994f-465b-989e-407c623f365f", "user": {"type": "User", "id": "2"}}, {"id": 2, "created": "2016-02-11T02:41:46.853000Z", "updated": null, "deleted": null, "uuid": "36e19c0e-bda6-4bd7-bc73-374a6fc509d6", "user": {"type": "User", "id": "3"}}, {"id": 3, "created": "2016-02-11T02:42:05.419000Z", "updated": null, "deleted": null, "uuid": "d0798ff4-3be2-4cf3-81ac-edf8049eb075", "user": {"type": "User", "id": "4"}}], "meta": {"pagination": {"page": 1, "pages": 1, "count": 3}}}
我希望输出具有数据属性而不是结果属性。我如何告诉 DRF JSON API 输出此处描述的样式:
我相信您必须创建自定义渲染器才能使用您的项目。这是来自 rest-framework renderers
的指南
我可以将您指向 json 模板的响应 class 中的行,以进行快速而肮脏的修复:
Response(), line 35
我的问题是我在视图中派生了一个视图集 class,如下所示:
class QuestionViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows questions and answers to be read.
"""
resource_name = 'questions'
queryset = Question.objects.all()
serializer_class = QuestionSerializer
renderers = renderers.JSONRenderer
parsers = parsers.JSONParser
这是错误的。 renderers 和 parsers 属性实际上分别是 renderer_classes 和 parser_classes。此外,这些属性的右值是 tuples,而不是 singletons。因此:
class QuestionViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows questions and answers to be read.
"""
resource_name = 'questions'
queryset = Question.objects.all()
serializer_class = QuestionSerializer
renderer_classes = (renderers.JSONRenderer,)
parser_classes = (parsers.JSONParser,)
此更改后,JSON API 响应基本正确:
{"data":{"type":"questions","id":"1","attributes":{"created":"2016-02-10T04:28:50.742000Z","updated":null,"deleted":null,"uuid":"eddfc27d-2677-49e5-bd37-92fecea340bd","text":"Are you dizzy?"},"relationships":{"answers":{"data":[{"type":"Answer","id":"1"},{"type":"Answer","id":"2"},{"type":"Answer","id":"3"}]},"members":{"data":[{"type":"Member","id":"1"},{"type":"Member","id":"2"},{"type":"Member","id":"3"},{"type":"Member","id":"4"}],"meta":{"count":4}}}}}
我有一个使用 Django Rest Framework 的 django 1.9.2 项目 JSON API:
https://github.com/django-json-api/django-rest-framework-json-api:
我的视图集如下所示:
class QuestionViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows questions and answers to be read.
"""
resource_name = 'questions'
queryset = Question.objects.all()
serializer_class = QuestionSerializer
renderers = renderers.JSONRenderer
parsers = parsers.JSONParser
典型的响应如下所示:
{"links": {"first": "http://testserver/api/v1/coaches?page=1", "last": "http://testserver/api/v1/coaches?page=1", "next": null, "prev": null}, "results": [{"id": 1, "created": "2016-02-11T02:41:22.569000Z", "updated": null, "deleted": null, "uuid": "0473c709-994f-465b-989e-407c623f365f", "user": {"type": "User", "id": "2"}}, {"id": 2, "created": "2016-02-11T02:41:46.853000Z", "updated": null, "deleted": null, "uuid": "36e19c0e-bda6-4bd7-bc73-374a6fc509d6", "user": {"type": "User", "id": "3"}}, {"id": 3, "created": "2016-02-11T02:42:05.419000Z", "updated": null, "deleted": null, "uuid": "d0798ff4-3be2-4cf3-81ac-edf8049eb075", "user": {"type": "User", "id": "4"}}], "meta": {"pagination": {"page": 1, "pages": 1, "count": 3}}}
我希望输出具有数据属性而不是结果属性。我如何告诉 DRF JSON API 输出此处描述的样式:
我相信您必须创建自定义渲染器才能使用您的项目。这是来自 rest-framework renderers
的指南我可以将您指向 json 模板的响应 class 中的行,以进行快速而肮脏的修复: Response(), line 35
我的问题是我在视图中派生了一个视图集 class,如下所示:
class QuestionViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows questions and answers to be read.
"""
resource_name = 'questions'
queryset = Question.objects.all()
serializer_class = QuestionSerializer
renderers = renderers.JSONRenderer
parsers = parsers.JSONParser
这是错误的。 renderers 和 parsers 属性实际上分别是 renderer_classes 和 parser_classes。此外,这些属性的右值是 tuples,而不是 singletons。因此:
class QuestionViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows questions and answers to be read.
"""
resource_name = 'questions'
queryset = Question.objects.all()
serializer_class = QuestionSerializer
renderer_classes = (renderers.JSONRenderer,)
parser_classes = (parsers.JSONParser,)
此更改后,JSON API 响应基本正确:
{"data":{"type":"questions","id":"1","attributes":{"created":"2016-02-10T04:28:50.742000Z","updated":null,"deleted":null,"uuid":"eddfc27d-2677-49e5-bd37-92fecea340bd","text":"Are you dizzy?"},"relationships":{"answers":{"data":[{"type":"Answer","id":"1"},{"type":"Answer","id":"2"},{"type":"Answer","id":"3"}]},"members":{"data":[{"type":"Member","id":"1"},{"type":"Member","id":"2"},{"type":"Member","id":"3"},{"type":"Member","id":"4"}],"meta":{"count":4}}}}}