简体   繁体   中英

Storing the customized django auth_user model

I am working on a website and for the login information, I am using a customized MyUser model that is a subclass of the original User model. Now, to create MyUser records, i use

x = MyUser.objects.create(
    username = #something,
    password = #something,
    ...
 )
 x.save()

etc. Now, the MyUser record is created. But when I go to my admin page, I see type of password as:

 Invalid password format or unknown hashing algorithm.

And authenticate(username,password) returns None and it works properly only if i go to admin and change the password. What is the best solution to store the MyUser data in the database?

Use the set_password functionality provided by django User

x = MyUser.objects.create(
    username = #something,
    ...
 )
x.set_password(#something)
x.save()

Now, set_password would apply the hashing algorithm, and store the encrypted password for you, where as what you are doing would just set the plain text password, which is the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM