[Fixed]-Django admin does not login properly with custom User model

10👍

Did you add following lines in to your create_superuser function which is under BaseUserManager? It might look like this:

class CustomUserManager(BaseUserManager):
    def create_user(self, username, email, password=None):
        if not username:
            raise ValueError('Dude you need a username.')
        if not email:
            raise ValueError(
                'type an e-mail..')

        new_user = self.model(
            username=username,
            email=CustomUserManager.normalize_email(email))
        new_user.set_password(password)
        new_user.save(using=self._db)
        return new_user

  def create_superuser(self, username, email, password):
        new = self.create_user(
            username,
            email,
            password=password
        )
        new.is_active = True
        new.is_staff = True
        new.is_superuser = True
        new.save(using=self._db)
        return new

Focus on:

    new.is_active = True
    new.is_staff = True
    new.is_superuser = True

2👍

I had the same issues but I found @alix’s answer so helpfully. I had forgotten to add is_active=True on create_superuser()

def create_staffuser(self, email, first_name=None, last_name=None, user_role=None, password=None):
    user = self.create_user(
        email,
        first_name=first_name,
        last_name=last_name,
        user_role=user_role,
        password=password,
        is_staff=True,
        is_active=True
    )
    return user

so basically you need to focus on is_active and check your

# changes the in-build User model to ours (Custom)
AUTH_USER_MODEL = 'accounts.User'

AUTHENTICATION_BACKENDS = (
    # Needed to login by custom User model, regardless of `allauth`
    "django.contrib.auth.backends.ModelBackend",

    # `allauth` specific authentication methods, such as login by e-mail
    "allauth.account.auth_backends.AuthenticationBackend",
)
👤toel

0👍

Yup, this part did the tick. Thanks for the pointers on these @gregoltsov

obj.is_active = True
obj.is_staff = True
obj.is_admin = True

What I was missing was the obj.is_active part.

0👍

The answer is :

def create_superuser(self, username, email, password=None, **extra_fields):
    user = self.create_user(username, email, password=password, is_staff=True, **extra_fields)
    user.is_active = True
    user.save(using=self._db)
    return

please vote if it works for you.

Leave a comment