Python 如何在Django Rest Framework JSON API中生成JSON-API数据属性与结果属性?

Python 如何在Django Rest Framework JSON API中生成JSON-API数据属性与结果属性?,python,django,django-rest-framework,json-api,Python,Django,Django Rest Framework,Json Api,我有一个使用django Rest Framework JSON API的django 1.9.2项目: : 我的视图集如下所示: class QuestionViewSet(viewsets.ReadOnlyModelViewSet): """ API endpoint that allows questions and answers to be read. """ resource_name = 'questions' queryset = Ques

我有一个使用django Rest Framework JSON API的django 1.9.2项目:

:

我的视图集如下所示:

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}}}
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
典型的响应如下所示:

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}}}
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
我希望输出具有数据属性,而不是结果属性。如何让DRF JSON API输出此处描述的样式:


我相信您必须创建一个自定义渲染器来处理您的项目。这是我们的导游

我可以向您指出json模板响应类中的一行,以获得快速而肮脏的修复:

我的问题是视图中有一个viewset派生类,如下所示:

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}}}
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和parser属性实际上分别是renderer_类和parser_类。此外,这些属性的右值是元组,而不是单例。因此:

在此更改之后,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}}}}}

谢谢你的回复。这有助于我调试上面提到的正确答案。所以问题与我建议的不同?我很困惑:D