Django error while creating superuser, AttributeError: 'Manager' object has no attribute 'get_by_natural_key'

Django error while creating superuser, AttributeError: 'Manager' object has no attribute 'get_by_natural_key'

我使用的是 Django 版本 1.11.3 和 djangorestframework 版本 3.6.3。在使用以下命令创建超级用户的阶段:

python manage.py createsuperuser

这个命令应该询问我的电子邮件和密码,虽然它确实询问了我的电子邮件,但在输入我的电子邮件后,我收到了一个错误:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/shivams334/myapp2/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/home/shivams334/myapp2/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/shivams334/myapp2/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/shivams334/myapp2/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in execute
    return super(Command, self).execute(*args, **options)
  File "/home/shivams334/myapp2/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/shivams334/myapp2/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 121, in handle
    self.UserModel._default_manager.db_manager(database).get_by_natural_key(username)
AttributeError: 'Manager' object has no attribute 'get_by_natural_key'

我的models.py是这样的:

from django.db import models
from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.models import BaseUserManager

class UserProfileManager(object):
    """helps django work with our custom user model"""
    def create_user(self,email,name,password=None):
        if not email:
            raise ValueError('User must have email')
        email = self.normalize_email(email)
        user = self.model(email=email, name=name)

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

        return user
    def create_superuser(self,email,name,password):
        """creates and saves a new superuser with given details"""
        user = self.create_user(email,name,password)
        user.is_superuser = True
        user.is_staff = True
        user.save(using=self._db)   

class UserProfile(AbstractBaseUser, PermissionsMixin):

    """docstring for UserProfile"""
    email = models.EmailField(max_length=255, unique=True)
    name = models.CharField(max_length=255)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)

    objects = UserProfileManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name']

    def get_full_name(self):
        """used to get a user full name"""
        return self.name

    def get_short_name():
        """used to get a users short name"""
        return self.name

    def __str__(self):
        """Django uses this when it needs to convert the object into string"""
        return self.email

而且我还更新了 settings.py 中的应用程序定义:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'profiles_api',
]
AUTH_USER_MODEL = 'profiles_api.UserProfile'

我尝试用 python2 和 python3 执行此操作,但错误是相同的。

UserProfileManager 应该继承自 BaseUserManager class,而不是 object:

class UserProfileManager(BaseUserManager):
...

您可以找到实现自定义用户模型的示例here