I came across this thread. As suggested in the comments I tried this code
class WritableSerializerMethodField(serializers.SerializerMethodField):
def __init__(self, method_name=None, **kwargs):
self.method_name = method_name
kwargs['source'] = '*'
super(serializers.SerializerMethodField, self).__init__(**kwargs)
def to_internal_value(self, data):
print('Data: ', data)
return {'profile': data}
It did the job. I decide to replace super(serializers.SerializerMethodField, self).__init__(**kwargs) with super().__init__(**kwargs) and I got KeyError
profile_data = validated_data.pop('profile')
KeyError: 'profile'
I realised this call is different in a way that first argument to super is serializers.SerializerMethodField not WritableSerializerMethodField.
What difference this makes?
More simplified code:
class One:
def func(self):
print("I am parent.")
class Two(One):
def func(self):
print("I am child.")
What is the difference between super(Two, Two()) and super(One, Two())?
CodePudding user response:
Those two super calls aren't equivalent.
super(serializers.SerializerMethodField, self).__init__()
calls __init__() of SerializerMethodField's parent - the grandparent of WritableSerializerMethodField, whereas
super().__init__()
just calls __init__() of WritableSerializerMethodField's parent, i.e., SerializerMethodField.
