6
Try:
class User(models.Model):
username = models.CharField()
password = models.CharField()
email = models.CharField()
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
class ProfileSerializer(serializers.ModelSerializer):
user = UserSerializer(many=True)
class Meta:
model = Profile
fields = ('user', 'profile_type',)
def create(self, validated_data):
user_data = validated_data.pop('user')
user = User.objects.create(**user_data)
profile = Profile.objects.create(user=user, **validated_data)
return profile
and check out
this for Writable nested serializers.
For dealing a nested object:
checkout this this
Source:stackexchange.com