2015-11-02 26 views
1

我正在使用django的身份驗證登錄用戶。但我有兩個模型從authenticate方法將檢查用戶憑據。一個是ApplicationUser,另一個是SystemUser我做了其中的一個,它工作正常,像這樣:如何在Django身份驗證中配置兩個AUTH_USER_MODEL

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_MODEL,怎麼辦authenticatelogin功能 知道在哪裏(ApplicationUserSystemUser)相匹配的憑證和用戶 相應

回答

0

創建會話在你的settings.py集

AUTH_USER_MODEL = 'yourapp.YouCustomUserModel' 

然後讓我們django d其餘的。

如果你想有其他用戶模型,你需要將它們從擴展您的選擇AUTH_USER_MODEL

+0

我已經做了,與'ApplicationUser'這是我CustomUserModel。我怎樣才能爲'SystemUser'指定呢? – Sibtain

+0

@Sibtain你可以只有一個認證模型,如果你想要從你的主要UserModel派生的其他模型,你需要擴展你的AUTH USER MODEL。 – levi

+0

你能舉一個粗略的例子嗎? – Sibtain