class UserSerializer serializers ModelSerializer profile ProfileSerial

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class UserSerializer(serializers.ModelSerializer):
profile = ProfileSerializer()
class Meta:
model = User
fields = ('username', 'email', 'profile')
def create(self, validated_data):
profile_data = validated_data.pop('profile')
user = User.objects.create(**validated_data)
Profile.objects.create(user=user, **profile_data)
return user