简体   繁体   English

自定义用户模型扩展AbstractUser,验证返回无

[英]Custom User Model extending AbstractUser, authenticate return None

I want create my custom user extending AbstractUser , but when i want authenticate my custom user return None. 我想创建扩展AbstractUser的自定义用户,但是当我想对自定义用户进行身份验证时,返回None。

When i create CustomUser() is stored in database but the passwowrd is not encrypted. 当我创建CustomUser()时,它存储在数据库中,但passwowrd未加密。 Can i use authenticate function of banckend default? 我可以使用banckend默认的身份验证功能吗? or i must create a custom backend for my custom user. 否则我必须为我的自定义用户创建一个自定义后端。

i added: 我补充说:

AUTH_USER_MODEL = 'mysite.customuser'

I think extending of AbstractUser, my class don't have same method or something is wrong 我认为是AbstractUser的扩展,我的类没有相同的方法,或者出了点问题

SOLVED! 解决了!

The problem was not to extend AbstracUser, the problem was when i saved the user, CustomUser(username='myusername', password='mypassword'), the password is saved like plain text and de function authenticate() doesn't work with this. 问题不是扩展AbstracUser,问题是当我保存用户CustomUser(username ='myusername',password ='mypassword')时,密码被保存为纯文本,并且de功能authenticate()无法使用这个。

It's necessary save user usinng UserCreationForm, or extending it, the most important is save() method because use set_password() method which set encrypted password so authenticate() method works. 必须使用UserCreationForm来保存用户或对其进行扩展,最重要的是save()方法,因为使用set_password()方法来设置加密密码,以便authenticate()方法起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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