如果我有两个序列化程序,其中一个是嵌套的,如何设置 restore_object 方法?例如,如果我定义了以下序列化程序,如何为我的嵌套序列化程序定义恢复对象字段?从文档中看如何处理这种情况并不明显。
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 = ?????