繁体   English   中英

Django:如何创建超级用户?

[英]Django: How to create a superuser?

再会,

我正在尝试使用 AbstractBaseUser 创建自定义用户 model。

模型.py

from django.db import models
from django.utils import timezone
from django.utils.translation import gettext as _
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser, PermissionsMixin

# Create your models here.


class CustomAccountManager(BaseUserManager):
    def create_user(self, email, username, password, **other):

        email = self.normalize_email(email)
        user = self.model(
            email=email,
            username=username,
            **other
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, username, password):

        user = self.create_user(
            email,
            password=password,
            username=username,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user


class NewUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email adress'), unique=True)
    username = models.CharField(max_length=100, unique=True)
    start_date = models.DateTimeField(default=timezone.now)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

    objects = CustomAccountManager()

    def __str__(self):
        return self.email

我使用文档来创建它。 https://docs.djangoproject.com/en/4.0/topics/auth/customizing/

我删除了我的数据库,进行了迁移,迁移并创建了一个超级用户,如屏幕截图所示: 在此处输入图像描述

使用密码“admin”和 email“admin@admin.com”。 但正如您在屏幕截图中看到的那样,它不起作用。

你知道这个问题吗?

非常感谢: :-)

编辑:settings.py

INSTALLED_APPS = [
    'abstractuser',

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

AUTH_USER_MODEL = 'abstractuser.NewUser'

is_staff字段决定用户是否可以登录管理站点。 在您的代码中,您没有在创建超级用户时将此属性设置为True

您需要在create_superuser function 中设置user.is_staff = Trueis_staff字段替换为从is_admin读取的属性。

def create_superuser(self, email, username, password):

    user = self.create_user(
        email,
        password=password,
        username=username,
    )
    user.is_admin = True
    user.is_staff = True  # can access the admin site
    user.save(using=self._db)
    return user

确保将以下方法添加到您的自定义用户 Model。 这些是 django-admin 面板所需的方法。 来源 - django

def has_perm(self, perm, obj=None):
    "Does the user have a specific permission?"
    # Simplest possible answer: Yes, always
    return True

def has_module_perms(self, app_label):
    "Does the user have permissions to view the app `app_label`?"
    # Simplest possible answer: Yes, always
    return True

@property
def is_staff(self):
    "Is the user a member of staff?"
    # Simplest possible answer: All admins are staff
    return self.is_admin

解决方案1:

class NewUser(AbstractBaseUser, PermissionsMixin):
     .
     .
     .
     @property
     def is_admin(self):
         if is_superuser and is_staff:
            return True
         return False

解决方案2:

def create_superuser(self, email, password, **other):
        other.setdefault('is_staff', True)
        other.setdefault('is_superuser', True)

        if other.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if other.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')
        return self._create_user(email, password, **other)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM