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

huangapple go评论118阅读模式
英文:

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

问题

我正在使用由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'
英文:

I am using a PolymorphicProxySerializer provided by drf_spectacular but I am getting a strange error when attempting to load the schema

usage

    @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)

serializers


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__"

Basically, if a boolean query parameter is added to the request, I will inject some dynamic data fetched from the jira api. I am trying to update the api docs to represent to two distinct possible schema

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

答案1

得分: 1

你需要在实例化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)
英文:

You need to pass many=True when you instantiate PolymorphicProxySerializer, since you're decorating a 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)

huangapple
  • 本文由 发表于 2023年6月6日 01:48:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定