3

I create a custom model for User, By this I can create superuser from command line, and its create successfully. But when I trying to login to Django admin with created Super user its show me This error enter image description here

@property def is_staff(self): return self.staff

@property def is_superuser(self): return self.superuser

@property def is_active(self): return self.active

These property also set True

models.py

from django.db import models
from django.contrib.auth.models import (BaseUserManager, AbstractBaseUser)

class UserManager(BaseUserManager):
    def create_user(self, email, password=None):
        """
        creates a user with given email and password
    """
    if not email:
        raise ValueError('user must have a email address')

    user = self.model(
        email=self.normalize_email(email),
    )

    user.set_password(password)
    user.save(self._db)
    return user

def create_staffuser(self, email, password):
    """
    creates a user with staff permissions
    """
    user = self.create_user(
        email=email,
        password=password
    )
    user.staff = True
    user.save(using=self._db)
    return user

def create_superuser(self, email, password):
    """
    creates a superuser with email and password
    """
    user = self.create_user(
        email=email,
        password=password
    )
    user.staff = True
    user.superuser = True
    user.save(using=self._db)
    return user


class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='Email address',
        max_length=255,
        unique=True
    )
active = models.BooleanField(default=False)
staff = models.BooleanField(default=False)  # <- admin user, not super user
superuser = models.BooleanField(default=False)  # <- super user


USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []  # <- email and password are required by default

class Meta:
    app_label = "account_app"
    db_table = "users"

def __str__(self):
    return self.email

def get_full_name(self):
    return str(self.email)

def has_perm(self, perm, obj=None):
    """Does the user has a specific permission"""
    return True

def has_module_perms(self, app_lable):
    """Does the user has permission to view a specific app"""
    return True

@property
def is_staff(self):
    """Is the user a staff member"""
    return self.staff

@property
def is_superuser(self):
    """Is the user a admin member"""
    return self.superuser

@property
def is_active(self):
    """Is the user active"""
    return self.active

# hook the user manager to objects
objects = UserManager()

settings.py where I change for Custom User Model

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'dashboard_app',
    'account_app',

]

AUTH_USER_MODEL = "account_app.User" # changes the built-in user model to ours

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
)
WSGI_APPLICATION = 'SMSystem.wsgi.application'

4 Answers 4

0

its set default value for active = 0 need to set 1 or True

Sign up to request clarification or add additional context in comments.

1 Comment

Can you please rephrase this answer? I did not get were I need to put active=1 or True. Thx
0

Had the same issue. Instead of password=None, change it to password. And pass 'password=password' together with 'username=username' into the create_user function as you see below:

class MyAccountManager(BaseUserManager): def create_user(self, email, username, password): if not email: raise ValueError('Please add an email address') if not username: raise ValueError('Please add an username')

    **user = self.model(email=self.normalize_email(
    email), username=username, password=password)**

    user.set_password(password)
    user.save(using=self._db)
    return user

    def create_superuser(self, email, username, password):
        user = self.create_user(email=self.normalize_email(
    email), username=username, password=password)

        user.is_active = True
        user.is_admin = True
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)
        return user

Hope it works for you

Comments

0

Only users with is_active attribute True can log into the system. Inside createuser method pass active=True or change default value of active to True

def create_user(self, email, password=None):
        """
        creates a user with given email and password
    """
    if not email:
        raise ValueError('user must have a email address')

    user = self.model(
        email=self.normalize_email(email),
        active=True, #Add this line
    )

    user.set_password(password)
    user.save(self._db)
    return user

--OR, change default value of active to true --

class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='Email address',
        max_length=255,
        unique=True
    )
    # change default value to True
    active = models.BooleanField(default=True)

Comments

0

What fixed this for me: Rather than passing password=password into the self.model call in my create_superuser method, I called user.set_password(password). After doing that, I was able to login.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.