django drf_spectacular.utils.PolymorphicProxySerializer.__init__()获得了意外的关键字参数“context”

zxlwwiss  于 2023-06-07  发布在  Go
关注(0)|答案(1)|浏览(167)

我正在使用由drf_spectacular提供的PolymorphicProxySerializer,但在尝试加载模式时出现奇怪的错误
用法

@extend_schema(
        parameters=[NoteQueryParameters],
        responses=PolymorphicProxySerializer(
            component_name="NoteSerializer",
            serializers=[NoteSerializer, NoteSerializerWithJiraData],
            resource_type_field_name=None,
        ),
    )
    def list(self, request, *args, **kwargs):
        return super().list(request, *args, **kwargs)

串行化器

class CleanedJiraDataSerializer(serializers.Serializer):
    key = serializers.CharField(max_length=20, allow_null=True)

class BugSerializer(serializers.Serializer):
    failures = serializers.CharField(max_length=10, required=False, allow_null=True)
    suite = serializers.CharField(max_length=100, required=False, allow_null=True)
    notes = serializers.CharField(max_length=1000, required=False, allow_null=True)
    tags = StringListField(required=False, allow_null=True, allow_empty=True)
    testCaseNames = StringListField(required=False, allow_null=True, allow_empty=True)
    testCaseIds = StringListField(required=False, allow_null=True, allow_empty=True)
    jira = CleanedJiraDataSerializer(required=False, allow_null=True)

class BugSerializerWithJiraData(BugSerializer):
    jira = serializers.DictField()

class NoteSerializer(serializers.ModelSerializer):
    bug = serializers.ListField(child=BugSerializer())

    class Meta:
        model = Notes
        fields = "__all__"

class NoteSerializerWithJiraData(serializers.ModelSerializer):
    bug = serializers.ListField(child=BugSerializerWithJiraData())

    class Meta:
        model = Notes
        fields = "__all__"

基本上,如果在请求中添加了一个布尔查询参数,我将注入一些从jira API获取的动态数据。我试图更新API文档,以表示两个不同的可能模式

PolymorphicProxySerializer.__init__() got an unexpected keyword argument 'context'
holgip5t

holgip5t1#

在示例化PolymorphicProxySerializer时,您需要传递many=True,因为您正在装饰ListView。

@extend_schema(
    parameters=[NoteQueryParameters],
    responses=PolymorphicProxySerializer(
        component_name="NoteSerializer",
        serializers=[NoteSerializer, NoteSerializerWithJiraData],
        resource_type_field_name=None,
        many=True
    ),
)
def list(self, request, *args, **kwargs):
    return super().list(request, *args, **kwargs)

相关问题