[英]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做这个,但错误是一样的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.