Serializing multiple models

6,672 views
Skip to first unread message

Lucas Simon

unread,
May 14, 2013, 4:22:52 PM5/14/13
to django-res...@googlegroups.com
Does the model serializer support serialization of multiple models? I want to have a UserProfile model that stores additional information and have a single endpoint for creating a new user and user profile. Let's say that my model

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    bio = models.TextField()
    age = models.IntegerField()

Is there a feature that supports something like this?

class NewUserSerializer(serializers.ModelSerializer)
    class Meta:
        model = User and UserProfile

Andy Zinsser

unread,
Jun 6, 2013, 1:39:12 PM6/6/13
to django-res...@googlegroups.com
You can specify serializers to user for model relationships.

class UserProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        fields = ('photo')


class UserSerializer(serializers.ModelSerializer):
    profile = UserProfileSerializer()

    class Meta:
        model = User
        fields = ('id', 'email', 'profile', 'username')

backe...@miv.uk.com

unread,
Jun 7, 2013, 7:07:15 AM6/7/13
to django-res...@googlegroups.com
But in that case you'll have a nested response such as:

{"username": xxx ,
"email": xxxx,
"profile": {"birthday": xxxx,"photo":xxx}
"..."
}
what about to get the response without nested:

{
"username": xxx
"email": xxx
"birthday":xxxx
"photo": xxxx
}

I will appreciate that!

Colin

unread,
Jun 21, 2013, 2:49:41 PM6/21/13
to django-res...@googlegroups.com
Since you don't care about the data hierarchy and just need a flat format, I assume this implies you don't use the same serializer for deserialization. If that's true, you should use Serializer instead of ModelSerializer. Below is an example.

Assume you have a UserProfile model, which has one-to-one relationship to the django User model, defined. The serializer you use defines how your data looks like (see below)

from rest_framework import serializers
from rest_framework.fields import Field
class UserSerializer(serializers.Serializer):
    username = Field(source='username')
    email = Field(source='email')
    birthday = Field(source='userprofile.birthday')
    photo = Field(source='userprofile.photo.url')

And then as for the corresponding view:

from rest_framework.views import APIView
from rest_framework.response import Response
class LoadUserView(APIView):
    def get(self, request, format=None):
        user = self.request.user
        serializer = InitMyAccountSerializer(user)
        return Response(serializer.data)

With the serializer and the view about, you should get:

{
"username": xxx
"email": xxx
"birthday":xxxx
"photo": xxxx
}


Colin
Message has been deleted

saham nadeem

unread,
Jul 24, 2018, 5:07:12 AM7/24/18
to Django REST framework
is does not work for me

saham nadeem

unread,
Jul 24, 2018, 5:07:43 AM7/24/18
to Django REST framework
Does not work for mw


Reply all
Reply to author
Forward
0 new messages