Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django hash password when create user from shell

I am using this example to create a custom User model. I want to test this through django shell.

When I create a user from django shell using MyUser.objects.create(email='[email protected]', data_of_birth=datetime.date.today(), password='somepassword'), the password doesn't get hashed and is stored in the database as plaintext.

But if I create a user through django admin portal, it gets stored as a hash.

What do I need to do so that it gets stored as hash even through shell? Do I need to implement some function of my own?

Django version 1.7.3

like image 284
Dharmit Avatar asked Oct 31 '25 05:10

Dharmit


2 Answers

Use User.set_password():

user = MyUser(email='[email protected]'), ...)
user.set_password('somepassword')
user.save()
like image 122
knbk Avatar answered Nov 01 '25 18:11

knbk


staff = Staff()
staff.username = username
staff.set_password(password)
staff.save()

here staff is inherited model from USER , when user enter password you just give to the set_password , django automaticaly converted into hasble pwd

like image 42
django-renjith Avatar answered Nov 01 '25 20:11

django-renjith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!