Python 如何在Django身份验证中配置两个AUTH_USER_模型
我正在使用django的身份验证来登录用户。但我有两个模型,其中Python 如何在Django身份验证中配置两个AUTH_USER_模型,python,django,django-authentication,Python,Django,Django Authentication,我正在使用django的身份验证来登录用户。但我有两个模型,其中authenticate方法将检查用户凭据。一个是ApplicationUser,另一个是SystemUser,我制作了其中一个,它可以像这样工作: 型号.py class UserManager(BaseUserManager): def create_user(self, email, password=None): """ Creates and saves a User with th
authenticate
方法将检查用户凭据。一个是ApplicationUser
,另一个是SystemUser
,我制作了其中一个,它可以像这样工作:
型号.py
class UserManager(BaseUserManager):
def create_user(self, email, password=None):
"""
Creates and saves a User with the given username and password.
"""
....
return user
def create_superuser(self, email, password):
...
return user
class ApplicationUser(AbstractBaseUser):
application_user_id = models.AutoField(primary_key=True)
....
....
def login_view(request):
...
user = authenticate(username = email, password = password)
if user is not None:
...
login(request, user)
....
....
视图.py
class UserManager(BaseUserManager):
def create_user(self, email, password=None):
"""
Creates and saves a User with the given username and password.
"""
....
return user
def create_superuser(self, email, password):
...
return user
class ApplicationUser(AbstractBaseUser):
application_user_id = models.AutoField(primary_key=True)
....
....
def login_view(request):
...
user = authenticate(username = email, password = password)
if user is not None:
...
login(request, user)
....
....
我遇到了这个问题,得到了一个答案,但我无法找到解决这个问题的办法
我的问题:
AUTH\u USER\u MODEL
,到目前为止,我已经设置了
ApplicationUser
asAUTH\u USER\u MODEL
AUTH\u USER\u模型
,如何验证
或登录
功能
知道在何处(ApplicationUser
或SystemUser
)匹配凭据并为用户创建会话
相应地在您的settings.py集合中
AUTH_USER_MODEL = 'yourapp.YouCustomUserModel'
那么剩下的就让django来做吧
如果你想拥有其他用户模型,你需要从你选择的
AUTH\u User\u模型扩展它们
我已经用ApplicationUser
做过了,这是我的CustomUserModel。我该如何指定SystemUser
的身份验证模型?@Sibtain您只能有一个身份验证模型,如果您希望从主用户模型派生其他模型,则需要扩展您的身份验证用户模型。您能给出一个粗略的示例吗?@Sibtain