我正在使用由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'
1条答案
按热度按时间holgip5t1#
在示例化PolymorphicProxySerializer时,您需要传递many=True,因为您正在装饰ListView。