I have a CustomUser model and am using Django Rest Framework. I have several needs before/after saving a user object. For example:
- Adding the user's email, first/last name to a mailchimp list using mail chimp's API.
- Hashing a user's password before saving.
I successfully do this in the UserSerializer class thus:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = CustomUser
fields = '__all__'
def create(self, validated_data):
groups = validated_data.pop('groups', None)
password = validated_data.pop('password', None)
user_permissions = validated_data.pop('user_permissions')
u = CustomUser.objects.create(
password=make_password(password), **validated_data)
u.groups.set(groups)
u.user_permissions.set(user_permissions)
return u
However, let's say I want to also create a user in the admin tool and not repeat this logic? Doesn't it make more sense to add this logic by overriding the CustomUser's save method, or is that problematic? What's the best solution for this use-case?