首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:“UserManager”对象没有特性“”create_superuser“”

AttributeError:“UserManager”对象没有特性“”create_superuser“”
EN

Stack Overflow用户
提问于 2015-11-12 06:32:11
回答 2查看 11.2K关注 0票数 4

我按照Django的方式设置了我的customUser管理器,但我仍然收到属性错误。我不确定还有什么是错误的。

代码语言:javascript
复制
class UserManager(BaseUserManager):
    def create_user(self, email, date_of_birth, password=None):
        """
        Creates and saves a User with the given email, date of
        birth and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

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

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

    def create_superuser(self, email, date_of_birth, password):
        """
        Creates and saves a superuser with the given email, date of
        birth and password.
        """
        user = self.create_user(email,
            password=password,
            date_of_birth=date_of_birth
        )
        user.is_admin = True
        user.save(using=self._db)
        return user

class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    date_of_birth = models.DateField()
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = UserManager()

    USERNAME_FIELD = 'email'

    def get_full_name(self):
        # The user is identified by their email address
        return self.email

    def get_short_name(self):
        # The user is identified by their email address
        return self.email

    def __str__(self):              # __unicode__ on Python 2
        return self.email

我已经定义了用户和超级用户。并将对象设置为UserManager。

回溯是:

代码语言:javascript
复制
Traceback (most recent call last):
  File "manage.py", line 10, in <module> execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 50, in execute
    return super(Command, self).execute(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 149, in handle
           self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
 AttributeError: 'UserManager' object has no attribute 'create_superuser'

我正在运行的代码是:

代码语言:javascript
复制
python manage.py createsuperuser
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-12 20:09:13

我以为你用的是Django 1.8。下面是创建继承AbstractUser模型的自己的模型所必须做的事情。

在您的模型中:

代码语言:javascript
复制
from django.contrib.auth.models import AbstractUser
from django.db import models

class User(AbstractUser):
  pass

对于你的User UserManager 模型,它会自动继承User of AbstractUser,这样你就不必写:objects = YourUserManager()了,除非你想要扩展它。在这种情况下,您可以这样做:

代码语言:javascript
复制
from django.contrib.auth.models import AbstractUser, UserManager as AbstractUserManager
from django.db import models

class UserManager(AbstractUserManager):
  pass

class User(AbstractUser):
    objects = UserManager()

在Django应用程序中使用的settings.py中定义用户模型:

代码语言:javascript
复制
# <module_name>.<user_model_name>
AUTH_USER_MODEL = 'user.User'

现在,当您想要在其他应用程序中导入您的用户模型以使用它时,请使用settings.AUTH_USER_MODEL。下面是一个示例:

代码语言:javascript
复制
from django.db import models
from django.conf import settings

class Token(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)

有关更多信息,请阅读正确的Django文档:Extending the existing user model

票数 3
EN

Stack Overflow用户

发布于 2018-07-16 23:37:40

尝试此操作,并根据您的要求替换字段:

代码语言:javascript
复制
class UserManager(BaseUserManager):

    def create_user(self, email, password=None,is_active=True,is_staff=False,is_admin=False):
        """
        Creates and saves a User with the given email and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

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

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

    def create_staffuser(self, email, password):
        """
        Creates and saves a staff user with the given email and password.
        """
        user = self.create_user(
            email,
            password=password,
        )
        user.staff = True
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password):
        """
        Creates and saves a superuser with the given email and password.
        """
        user = self.create_user(
            email,
            password=password,
        )
        user.staff = True
        user.admin = True
        user.save(using=self._db)
        return user
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33661112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档