If I have two serializers, where one is nested, how do I setup the restore_object method? For example, if I have the following serializers defined, how do I define the restore object field for my nested serializer? It is not obvious from the documentation how to handle such a case.
class UserSerializer(serializers.Serializer):
first_name = serializers.CharField(required=True, max_length=30)
last_name = serializers.CharField(required=True, max_length=30)
username = serializers.CharField(required=True, max_length=30)
email = serializers.EmailField(required=True)
password = serializers.CharField(required=True)
def restore_object(self, attrs, instance=None):
if instance:
instance.first_name = attrs.get('first_name', instance.first_name)
instance.last_name = attrs.get('last_name', instance.last_name)
instance.email = attrs.get('email', instance.email)
instance.password = attrs.get('password', instance.password)
class UserProfileSerializer(serializers.Serializer):
user = UserSerializer()
bio = serializers.CharField()
def restore_object(self, attrs, instance=None):
if instance:
instance.bio = attrs.get('bio', instance.bio)
instance.user = ?????