Pallavi Singh
2018-06-07 07:48:28 UTC
---------- Forwarded message ----------
From: Pallavi Singh <***@arimaresearch.com>
Date: Tue, Jun 5, 2018 at 10:47 PM
Subject: nested serializer
To: Django REST framework <django-rest-***@googlegroups.com>
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('url', 'id', 'username', 'last_name', 'email', 'password')
extra_kwargs = {'password': {'write_only': True}}
class UserProfileSerializer(serializers.ModelSerializer):
user = UserSerializer(required=True)
class Meta:
model = UserProfile
fields = ('user', 'contact_no', 'token_key', 'group',)
def create(self, validated_data):
"""
Overriding the default create method of the Model serializer.
:param validated_data: data containing all the details of profile
:return: returns a successfully created profile record
"""
user_data = validated_data.pop('user')
user = UserSerializer.create(UserSerializer(), validated_data=user_data)
profile, created =
UserProfile.objects.get_or_create(user=user,
contact_no=validated_data.pop('contact_no'),
token_key=validated_data.pop('token_key'))
return profile
class GroupSerializer(serializers.ModelSerializer):
class Meta:
model = Group
fields = ('user_profile','id', 'name', )
def create(self, validated_data):
group = Group(name=validated_data["name"],)
group.save()
return group
i want data of GroupSerializer (id,name) i json format in group field of
userprofileserializer.in group field now i m geting group_id.the user,and
group model have foriegn key relation with userprofile model.
i want data in get request.help me pls.
From: Pallavi Singh <***@arimaresearch.com>
Date: Tue, Jun 5, 2018 at 10:47 PM
Subject: nested serializer
To: Django REST framework <django-rest-***@googlegroups.com>
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('url', 'id', 'username', 'last_name', 'email', 'password')
extra_kwargs = {'password': {'write_only': True}}
class UserProfileSerializer(serializers.ModelSerializer):
user = UserSerializer(required=True)
class Meta:
model = UserProfile
fields = ('user', 'contact_no', 'token_key', 'group',)
def create(self, validated_data):
"""
Overriding the default create method of the Model serializer.
:param validated_data: data containing all the details of profile
:return: returns a successfully created profile record
"""
user_data = validated_data.pop('user')
user = UserSerializer.create(UserSerializer(), validated_data=user_data)
profile, created =
UserProfile.objects.get_or_create(user=user,
contact_no=validated_data.pop('contact_no'),
token_key=validated_data.pop('token_key'))
return profile
class GroupSerializer(serializers.ModelSerializer):
class Meta:
model = Group
fields = ('user_profile','id', 'name', )
def create(self, validated_data):
group = Group(name=validated_data["name"],)
group.save()
return group
i want data of GroupSerializer (id,name) i json format in group field of
userprofileserializer.in group field now i m geting group_id.the user,and
group model have foriegn key relation with userprofile model.
i want data in get request.help me pls.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.