Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 = ?????
like image 733
user1876508 Avatar asked May 10 '13 21:05

user1876508


People also ask

How do you serialize an object in Django REST framework?

Creating and Using Serializers To create a basic serializer one needs to import serializers class from rest_framework and define fields for a serializer just like creating a form or model in Django.

What is serialization in Django REST framework?

Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON , XML or other content types.

How do I pass Queryset to serializer?

To serialize a queryset or list of objects instead of a single object instance, you should pass the many=True flag when instantiating the serializer. You can then pass a queryset or list of objects to be serialized.

What is SlugRelatedField Django?

SlugRelatedField. SlugRelatedField may be used to represent the target of the relationship using a field on the target. For example, the following serializer: class AlbumSerializer(serializers. ModelSerializer): tracks = serializers.


1 Answers

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:

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

or to serialize more than one object:

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

Foreign keys and m2m relations are then stored in an array of ids.

If you want only a subset of fields to be serialized:

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

To save the user profile you would just write:

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

The user id is saved in the serialized data and looks like this:

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

If you want related user fields in the serialization too, you need to modify 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 with natural keys you need to add the use_natural_keys argument:

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

Which leads to the following output:

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

Deserialization

Deserializing and saving is just as easy 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

like image 142
Alp Avatar answered Sep 27 '22 16:09

Alp