7👍
✅
For anyone doing a OneToOne mapping on a key this is the correct code –
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ('company', 'is_admin', 'last_modified', 'uuid')
class UserSerializer(serializers.ModelSerializer):
profile = UserProfileSerializer()
class Meta:
model = User
fields = ('url', 'username', 'email', 'profile')
def create(self, validated_data):
profile_data = validated_data.pop('profile')
user = User.objects.create(**validated_data)
UserProfile.objects.create(user=user, **profile_data)
return user
Note that the following is removed for profile_data in profile_data:
Reason being there isn’t multiple values.
Source:stackexchange.com