我正在尝试在Django 1.6中实现我自己的自定义用户模型,但是我收到了这个错误。
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 141, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
TypeError: create_superuser() missing 1 required positional argument: 'email'
这是我的UserManager
class UserManager(BaseUserManager):
def _create_user(self, username, email, password, is_staff, is_superuser, **extra_fields):
now = timezone.now()
email = self.normalize_email(email)
user = self.model(username=username, email=email,
is_staff=is_staff, is_active=False,
is_superuser=is_superuser, last_login=now,
date_joined=now, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, username, email=None, password=None, **extra_fields):
return self._create_user(username, email, password, False, False,
**extra_fields)
def create_superuser(self, username, email, password, **extra_fields):
user=self._create_user(username, email, password, True, True,
**extra_fields)
user.is_active=True
user.save(using=self._db)
return user
这似乎相当直接,但我似乎无法弄清楚为什么我首先收到此错误,因为我确实在create_superuser函数中指定了电子邮件。我已经在线查看了几个教程,但无法看到这是如何以不同的方式实现的。有人可以解释我做错了吗?
答案 0 :(得分:12)
查看管理命令的the code,它仅提示用户模型的REQUIRED_FIELDS
属性(以及用户名)中的字段。该属性默认包含在AbstractBaseUser中的电子邮件,但如果您已覆盖它 - 或者首先不从该模型继承(您应该这样做) - 那么将不会提示电子邮件,也不会将其传递给create_superuser方法。 / p>
答案 1 :(得分:3)
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField('username', max_length=150, unique=True)
email = models.EmailField('email address', unique=True, max_length = 255)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
对我而言,上面的代码只是解决了问题。