drf-spectaulous 生成 dict 数组而不是 dict

问题描述 投票:0回答:1

这是我使用 drf-spectaulous 进行 GET 请求,用于生成 swagger:

@extend_schema(request=None,response=RespondentResponseTransactionalWithQuestionGetSerializer(many=False)
     )
     
   def get(self, request, *args, **kwargs):

这是序列化器本身:

class RespondentResponseTransactionalWithQuestionGetSerializer(serializers.Serializer):
    answers = RespondentResponseTransactionalGetSerializer(many=True)
    general_question = GeneralQuestionResultTransactionalPostSerializer(required=False)

我希望生成一个与此类似的对象:

{
  "general_question": {
    "id": 0,
    "value": "string",
    "employee": 0,
    "general_question": 0
  },
  "answers": [
    {
      "employee": 0,
      "activity": 0,
      "fte": 99999,
      "values": [
        {
          "value": 2147483647,
          "subdimension": 0
        }
      ],
      "custom_fields": [
        {
          "value": "string",
          "custom_field": 0
        }
      ]
    }
  ]
}

但是根据 swagger 文档,我得到了一组类似的对象:

[    {
      "general_question": {
        "id": 0,
        "value": "string",
        "employee": 0,
        "general_question": 0
      },
      "answers": [
        {
          "employee": 0,
          "activity": 0,
          "fte": 99999,
          "values": [
            {
              "value": 2147483647,
              "subdimension": 0
            }
          ],
          "custom_fields": [
            {
              "value": "string",
              "custom_field": 0
            }
          ]
        }
      ]
    }]

关于如何使其看起来正确有什么想法吗?我尝试过很多=假,但没有很多,但似乎对我不起作用。

python django swagger swagger-codegen drf-spectacular
1个回答
0
投票

问题出在视图中: 视图被定义为 generic.ListAPIView 而不是 RetrieveView。

© www.soinside.com 2019 - 2024. All rights reserved.