I'm new at Django Rest Framework and I wonder how methods create and update are called inside a ModelSerializer Class.
What I understand is that create and update methods are automatically triggered when a POST or PUT request is sent. Is there a way to call a new function, like say_hello()?
Here's an example. I want to call method say_hello() when I hit a PUT Request
class UserSerializer(serializers.ModelSerializer):
"""Serializer for the users object"""
class Meta:
model = User
fields = "__all__"
def create(self, validated_data):
"""Create a new user with encrypted password and return it"""
return get_user_model().objects.create_user(**validated_data)
def update(self, instance, validated_data):
"""Update an user, setting the password and return it"""
password = validated_data.pop('password', None)
user = super().update(instance, validated_data)
if password:
user.set_password(password)
user.save()
return user
def say_hello(self, validated_data):
print(validated_data)
return "Hello you"
say_hello(...)method from theupdate()method, right?perform_create(self, serializer)andperform_update(self, serializer).