如何在Django身份验证中配置两个AUTH_USER_MODEL

时间:2015-11-02 17:30:31

标签: python django django-authentication

我正在使用django的身份验证来登录用户。但我有两个模型,其中authenticate方法将检查用户凭据。一个是ApplicationUser,另一个是SystemUser我已经制作了其中一个,它的效果很好:

models.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)
    ....
    ....

views.py

def login_view(request):
     ...
     user = authenticate(username = email, password = password)
     if user is not None:
         ...
         login(request, user)
         ....
         ....

我遇到了这个问题而得到了here,但我无法解决这个问题。

我的问题:

  1. 如何设置两个AUTH_USER_MODEL,我已经设置了 ApplicationUserAUTH_USER_MODEL

  2. 即使我以某种方式指明     这两个AUTH_USER_MODELauthenticatelogin如何运作     知道在哪里(ApplicationUserSystemUser)匹配凭证并为用户创建会话     相应

1 个答案:

答案 0 :(得分:0)

在settings.py中设置

CDbl

然后让django做其余的事情。

如果您想要其他用户模型,则需要从所选的AUTH_USER_MODEL = 'yourapp.YouCustomUserModel'

扩展它们
相关问题