簡體   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