manage.py createsuperuser不适用于自定义用户模型

时间:2018-07-22 07:28:30

标签: python django

我正在使用Django,并且已经创建了一个自定义用户模型,能够使用电子邮件代替用户名作为主要登录方法。一切正常,但是我无法使用命令{{ 1}}。

这是我的代码:

models.py

python manage.py createsuperuser

这是 managers.py

from django.db import models
from django.core.mail import send_mail
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.base_user import AbstractBaseUser
from django.db.models import CASCADE
from django.utils.translation import ugettext_lazy as _
from .managers import UserManager

    class User(AbstractBaseUser, PermissionsMixin):
        user_name = models.CharField(_('user name'), max_length=35)
        email = models.EmailField(_('email address'), unique=True)
        date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)
        is_active = models.BooleanField(_('active'), default=True)
        is_staff = models.BooleanField(_('staff'), default=False)
        is_superuser = models.BooleanField(_('superuser'), default=False)
        avatar = models.ImageField(upload_to='media/', null=True, blank=True)

        objects = UserManager()


        USERNAME_FIELD = 'email'
        REQUIRED_FIELDS = []


        class Meta:
            verbose_name = _('user')
            verbose_name_plural = _('users')

        def email_user(self, subject, message, from_email=None, **kwargs):
            '''
            Sends an email to this User.
            '''
            send_mail(subject, message, from_email, [self.email], **kwargs)

该过程将创建一个普通用户而不是superuser,并将其创建。 但是我无法登录管理面板from django.contrib.auth.base_user import BaseUserManager class UserManager(BaseUserManager): use_in_migrations = True def _create_user(self, email, password, **extra_fields): """ Creates and saves a User with the given email and password. """ if not email: raise ValueError('The given email must be set') email = self.normalize_email(email) user = self.model(email=email, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_user(self, email, password=None, **extra_fields): extra_fields.setdefault('is_superuser', False) return self._create_user(email, password, **extra_fields) def create_superuser(self, email, password, **extra_fields): extra_fields.setdefault('is_superuser', True) if extra_fields.get('is_superuser') is not True: raise ValueError('Superuser must have is_superuser=True.') return self._create_user(email, password, **extra_fields)

2 个答案:

答案 0 :(得分:1)

将此字段添加到用户模型

is_admin = models.BooleanField(default=False)

并在UserManager中设置is_admin = True

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

对于您的超级用户,请确保is_stafftrue,因此将这些行添加到您的User模型中

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

尝试一下,看看是否可行

答案 1 :(得分:1)

您可以像下面那样实现UserManager。它比您的代码简单。

class UserManager(BaseUserManager):

    def create_user(self, email, password=None):

        if email is None:
            raise TypeError('Users must have an email address.')

        user = self.model(email=self.normalize_email(email))
        user.set_password(password)
        user.save()

        return user

    def create_superuser(self, email, password):

        if password is None:
            raise TypeError('Superusers must have a password.')

        user = self.create_user(email, password)
        user.is_superuser = True
        user.is_staff = True
        user.save()

        return user