我正在使用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做这个,但错误是一样的。
答案 0 :(得分:3)
UserProfileManager
应该从BaseUserManager
课程继承,而不是从object
继承:
class UserProfileManager(BaseUserManager):
...
您可以找到实施自定义用户模型here
的示例