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 = ?????
Important sidenote: It looks like you are using the old User/Profile methodology. Since Django 1.5 there is no seperation of the default User and your custom Profile models. You have to create your own user model and use that instead of the default one: Custom User Profile @ Django Docs
Serialization
I want to present you a different approach to serialize and restore models. All model objects can be serialized by using the following snippet:
or to serialize more than one object:
Foreign keys and m2m relations are then stored in an array of ids.
If you want only a subset of fields to be serialized:
To save the user profile you would just write:
The user id is saved in the serialized data and looks like this:
If you want related user fields in the serialization too, you need to modify your User model:
When serializing with natural keys you need to add the
use_natural_keys
argument:Which leads to the following output:
Deserialization
Deserializing and saving is just as easy as:
More information can be found in the Django docs: Serializing Django objects