django not hashing password custom user

Question:

i am working on a model which inherits from from django.contrib.auth.models import AbstractBaseUser

earlier i was specifying password field like:

password = models.Charfield(max_length=150)

but then i need to specify it as:

create_password = models.CharField(max_length=150)

in earlier case i was getting password in hashed format.
but after changing password to create_password
the value is saving in unhashed format (originally entered value).

all other settings are same as before, i am using user.set_password(create_password) function . but still no hashing.

how can i hash create_password field?

Asked By: harshit

||

Answers:

The set password function in Django AbstractBaseUser expects the password field name to be ‘password’ only, this is how the function looks if you go inside the AbstractBaseUser class:

def set_password(self, raw_password):
    self.password = make_password(raw_password)
    self._password = raw_password

But if you for some reason need your password field to be create_password instead of ‘password’ you can override the ‘set_password’ function in your user model like this:

def set_password(self, raw_password):
    self.create_password = make_password(raw_password)
    self._password = raw_password

I am assuming you have set your user model in the settings.py file and have created the Manager class as well.

Answered By: Dakshesh Jain
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.