我有一个CustomUser
,它将AbstractUser
扩展如下
class CustomUser(AbstractUser):
USERTYPE_CHOICES = [(1,"A"),(2,"B"),(3,"C")]
usertype = models.IntegerField(choices = USERTYPE_CHOICES, default = 1)
我的settings.py
INSTALLED_APPS = [
...
'login_register_service_hub.apps.LoginRegisterServiceHubConfig',
...
AUTH_USER_MODEL = 'login_register_service_hub.CustomUser'
一切正常,但一切正常:
我正在尝试实现自定义身份验证后端,并且通过添加以下内容
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth import get_user_model
#Check for the is_active property
class EmailBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
UserModel = get_user_model()
try:
user = UserModel.objects.get(email=username)
except UserModel.DoesNotExist:
return None
else:
if user.check_password(password):
return user
return None
def get_user(self, user_id):
UserModel = get_user_model()
try:
return UserModel.objects.get(pk=user_id)
except UserModel.DoesNotExist:
return None
我用以下内容更新settings.py
:
AUTHENTICATION_BACKENDS = {'login_register_service_hub.EmailBackend',}
但是,只要我跑步
python3 manage.py runserver
我收到以下错误
"AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'login_register_service_hub.CustomUser' that has not been installed
有趣的是,仅当我导入ModelBackend
时命令才会失败(即使我注释掉EmailBackend
定义也是如此)
答案 0 :(得分:1)
您通常 不要在__init__.py
应用中导入内容。将您的后端移至login_register_service_hub/backends.py
,然后更新您的设置
AUTHENTICATION_BACKENDS = ['login_register_service_hub.backends.EmailBackend']