How to serialize Django models with nested objects (Django REST Framework)

If I have two serializers, where one is nested, how do I configure restore_object? For example, if I have the following serializers installed, how do I determine the recovery object field for my nested serializer? The documentation does not show how to handle this 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 = ????? 
+7
source share
1 answer

Important Alert: It appears that you are using the old User / Profile methodology. Starting with Django 1.5, there are no separate user and user profile models. You must create your own user model and use it instead of the standard one: @Django Docs User User Profile

Serialization

I want to introduce you to a different approach to serializing and restoring models. All model objects can be serialized using the following snippet:

 from django.core import serializers serialized_data = serializers.serialize("json", myInstance) 

or serialize more than one object :

 serialized_data = serializers.serialize("json", User.objects.all()) 

The foreign keys and m2m relationships are then stored in an identifier array.

If you want to serialize only a subset of fields :

 serialized_data = serializers.serialize("json", myUserInstance, fields=('first_name ','last_name ','email ','password ')) 

To save a user profile, you just have to write:

 serialized_data = serializers.serialize("json", myUserProfileInstance) 

The user ID is stored in serialized data and looks like this:

 { "pk": 1, "model": "profile.UserProfile", "fields": { "bio": "self-taught couch potato", "user": 1 } } 

If you want user related fields in serialization too, you need to change your user model:

 class UserManager(models.Manager): def get_by_natural_key(self, first_name, last_name): return self.get(first_name=first_name, last_name=last_name) class User(models.Model): objects = UserManager() first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) ... def natural_key(self): return (self.first_name, self.last_name) class Meta: unique_together = (('first_name', 'last_name'),) 

When serializing using natural keys, you need to add the use_natural_keys argument:

 serialized_data = serializers.serialize("json", myUserProfileInstance, use_natural_keys=True) 

This leads to the following conclusion:

 { "pk": 2, "model": "profile.UserProfile", "fields": { "bio": "time-traveling smartass", "user": ["Dr.", "Who"] } } 

Deserialization

Deserializing and saving is as simple as:

 for deserialized_object in serializers.deserialize("json", serialized_data): deserialized_object.save() 

More information can be found in the Django docs: Serializing Django Objects

+22
source

All Articles