2

I'm using django's authentication to login users. But I have two models from where authenticate method would check the user credentials. One is ApplicationUser and the other is SystemUser I have made one of them and it works fine like so:

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

I came through this problem and got here but I couldn't work out a solution to this.

My Questions:

  1. How do I specify two AUTH_USER_MODEL, as of yet I have set ApplicationUser as AUTH_USER_MODEL.

  2. And even if I somehow specify the two AUTH_USER_MODEL, how do the authenticate or login function know where (ApplicationUser or SystemUser) to match the credentials and create session for the user accordingly

Community
  • 1
  • 1
Sibtain
  • 1,436
  • 21
  • 39

1 Answers1

0

In your settings.py set

AUTH_USER_MODEL = 'yourapp.YouCustomUserModel'

Then let's django do the rest.

If you want to have others User Model, you need to extend them from your selected AUTH_USER_MODEL

levi
  • 22,001
  • 7
  • 73
  • 74
  • I have already done that with `ApplicationUser` which is my CustomUserModel. How would I specify that for `SystemUser` also? – Sibtain Nov 02 '15 at 17:38
  • @Sibtain you can have only one auth model, if you want others models derived from your main UserModel, you need extend your AUTH USER MODEL. – levi Nov 02 '15 at 17:40
  • Can you give a rough example for that? – Sibtain Nov 02 '15 at 17:42
  • @Sibtain http://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django – levi Nov 02 '15 at 18:18