我有一个调用其他序列化程序的序列化程序,而另一个序列化程序是一个自定义序列化程序,它覆盖了 .to_representation() 行为。如果我无法访问从视图发送到第一个序列化程序的数据,我应该如何从第一个序列化程序调用这个自定义序列化程序?
这是情况的一个简单示例:
class OtherSerializer(serializers.Serializer):
def to_representation(self, obj):
# ... can't get data
class NestedSerializer(serializers.Serializer):
someotherfield = OtherSerializer(somedata, many=True) # this fails because obviously can't read "somedata", how to get access to it?
boolfield = BooleanField()
NestedSerializer(data={'someotherfield': somedata, 'boolfield': False}) # this somedata is the one I'm talking above