[Answered ]-Django is hashing my passwords different when creating a User vs trying to authenticate a user

1👍

The ! at the start of the password value has a special meaning in Django – it means that an unusable password has been set for the user – the rest of the value is just a random string that will never be successfully validated.

So the question is why is an unusable password being set? There are two possibilities I can see from your code:

  1. UserManager.create_super_user doesn’t set the user’s password at all – if you are using this to create users, then no password will be set for them.

  2. If you’re using the CreateUserSerializer, then it may be that the value of password is None – we would need to see the serializer definition to confirm whether a null value would be considered valid. I think this is the most likely issue. Passing None to create_user will cause set_password to set an unusable password. You then need to investigate why an empty value is being passed to the serializer.

0👍

The problem was what solarissmoke proposed with the CreateUserSerializer. I had my password set to write only which wasn’t letting my view to get to password, instead it was returning None.

I changed my view from this:

class CreateUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('email', 'password')
        extra_kwargs = {
            'password' : {'write_only': True}
        }

To this (corrected version):

class CreateUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('email', 'password')

Leave a comment