mardi 6 mai 2014

python - champ de mot de passe changement Django modèle d'authentification - Stack Overflow


I am trying to modify my Django project authentication so I can use my own User model.


I have got it working so far, however I am unable to override the "password" field. I want to change the name to "password_hash".


I have tried this manager:


class UserManager(BaseUserManager):
def create_user(self, email_address, full_name, password=None):

if not email_address:
raise ValueError('Users must have an email address')

user = self.model(
email_address = self.normalize_email(email_address),
full_name = full_name,
)

user.set_password(password)
user.save(using=self._db)
return user

def create_superuser(self, email_address, full_name, password_hash):
user = self.create_user(email_address,
password_hash=password_hash,
full_name=full_name,
)
user.is_admin = True
user.save(using=self._db)
return user

However I get the error TypeError: create_superuser() got an unexpected keyword argument 'password'.


How do I stop create_superuser() from expecting "password" and change it to "password_hash". For username I did it by changing the USERNAME_FIELD, however nothing in the documentation suggests a similar method for the password.


Thanks in advance, Mark




The solution that worked for me was to override this method in my new user model:


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


I am trying to modify my Django project authentication so I can use my own User model.


I have got it working so far, however I am unable to override the "password" field. I want to change the name to "password_hash".


I have tried this manager:


class UserManager(BaseUserManager):
def create_user(self, email_address, full_name, password=None):

if not email_address:
raise ValueError('Users must have an email address')

user = self.model(
email_address = self.normalize_email(email_address),
full_name = full_name,
)

user.set_password(password)
user.save(using=self._db)
return user

def create_superuser(self, email_address, full_name, password_hash):
user = self.create_user(email_address,
password_hash=password_hash,
full_name=full_name,
)
user.is_admin = True
user.save(using=self._db)
return user

However I get the error TypeError: create_superuser() got an unexpected keyword argument 'password'.


How do I stop create_superuser() from expecting "password" and change it to "password_hash". For username I did it by changing the USERNAME_FIELD, however nothing in the documentation suggests a similar method for the password.


Thanks in advance, Mark



The solution that worked for me was to override this method in my new user model:


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

0 commentaires:

Enregistrer un commentaire