无法使用自定义用户模型登录到 Django Admin

时间:2021-02-13 07:27:12

标签: django django-admin django-users django-login django-custom-user

我为用户创建了一个自定义模型,通过这个我可以从命令行创建超级用户,并成功创建。但是,当我尝试使用创建的超级用户登录 Django 管理员时,它会显示此错误 enter image description here

@property def is_staff(self): 返回self.staff

@property def is_superuser(self): 返回 self.superuser

@property def is_active(self): 返回 self.active

这些属性也设置为 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 我更改自定义用户模型的位置

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'

1 个答案:

答案 0 :(得分:0)

它为 active = 0 设置的默认值需要设置为 1 或 True