In Django DRF, I can add the following method to a serializer:
def create(self, validated_data):
user = User(
email=validated_data['email'],
username=validated_data['username']
)
user.set_password(validated_data['password'])
user.save()
return user
Previous to discovering this, I thought you typically did this within a view, by overriding a View/ViewSet methods.
I had thought that all the Serializer did was to transform the data and send it out or receive it from external calls.
Can someone explain to me the difference between doing this on the view vs doing it on a serializer?
And more broadly, what is the benefit of calling methods on the serializer instead of somewhere else?