Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
730 B
1
Indexable
Never
class UpdateUserSerializer(serializers.ModelSerializer):
    profile = ProfileSerializer()
    class Meta:
        model = get_user_model()
        fields = ['first_name', 'last_name', 'profile']

    def update(self, instance, validated_data):
        # We try to get profile data
        profile_data = validated_data.pop('profile', None)
        # If we have one
        if profile_data is not None:
            # We set address, assuming that you always set address
            # if you provide profile
            instance.profile.address = profile_data['address']
            # And save profile
            instance.profile.save()
        # Rest will be handled by DRF
        return super().update(instance, validated_data)