简体   繁体   English

创建超级用户时出现Django错误,AttributeError:'Manager'对象没有属性'get_by_natural_key'

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

I am using Django version 1.11.3 and djangorestframework version 3.6.3. 我使用的是Django版本1.11.3和djangorestframework版本3.6.3。 At the stage of creating the superuser with the following command: 在使用以下命令创建超级用户的阶段:

python manage.py createsuperuser

This command was supposed to ask me about my Email and Password, though it does ask me the Email but after entering my Email, I got an error: 这个命令应该问我关于我的电子邮件和密码,虽然它确实问我电子邮件,但在输入我的电子邮件后,我收到一个错误:

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'

My models.py is this: 我的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

And I also updated the Application Definition in settings.py: 我还在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'

I have tried to do this with both python2 and python3 but the error is same. 我试过用python2和python3做这个,但错误是一样的。

UserProfileManager should be inherited from BaseUserManager class, not from object : UserProfileManager应该从BaseUserManager类继承,而不是从object继承:

class UserProfileManager(BaseUserManager):
...

You can find example of implementation custom user model here 您可以在此处找到实现自定义用户模型的示例

暂无
暂无

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

相关问题 AttributeError: 'Manager' object has no attribute 'get_by_natural_key' 错误在Django? - AttributeError: 'Manager' object has no attribute 'get_by_natural_key' error in Django? 在 _validate_username AttributeError: 'Manager' object has no attribute 'get_by_natural_key' 错误 - in _validate_username AttributeError: 'Manager' object has no attribute 'get_by_natural_key' error Django 创建超级用户错误:AttributeError: &#39;ProfileManager&#39; 对象没有属性 &#39;create_superuser&#39; - Django creating super user error: AttributeError: 'ProfileManager' object has no attribute 'create_superuser' Django-get_by_natural_key()恰好接受3个参数(给定2个) - Django - get_by_natural_key() takes exactly 3 arguments (2 given) Django错误:“管理器”对象上的AttributeError没有属性“ create_user” - Django error: AttributeError at 'Manager' object has no attribute 'create_user' get_by_natural_key 和 natural_key 的区别 - Difference between get_by_natural_key and natural_key 如何在Django中的ContentType外键上使用get_by_natural_key()加载数据夹具? - How to load a data fixture with get_by_natural_key() on ContentType foreign key in Django? 尝试使用 Django 设置 SendGrid 的电子邮件 API 时出现此错误 - AttributeError: &#39;str&#39; object has no attribute &#39;get&#39; - Getting this error while trying to set SendGrid's email API with Django - AttributeError: 'str' object has no attribute 'get' AttributeError: &#39;Manager&#39; 对象没有属性 - AttributeError: 'Manager' object has no attribute Django AttributeError:“ tuple”对象没有属性“ get” - Django AttributeError: 'tuple' object has no attribute 'get'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM