问题描述
我正在尝试在 Django 1.6 中实现我自己的自定义用户模型,但出现此错误.
I am trying to implement my own custom user model in Django 1.6 but I am getting this error.
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'
这是我的用户管理器
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 函数中指定了电子邮件.我在网上浏览了几个教程,看不出这是如何以不同的方式实现的.有人可以解释一下我做错了什么吗?
It seems like this would be fairly straight forward but I can't seem to figure out why I am getting this error in the first place because I do have email specified in my create_superuser function. I have looked through several tutorials online and can't see how this is implemented differently. Can someone explain what I am doing wrong?
推荐答案
看代码 用于管理命令,它只提示输入用户模型的REQUIRED_FIELDS
属性(以及用户名)中的字段.该属性在 AbstractBaseUser 中默认包含电子邮件,但如果您已覆盖它 - 或者不是首先从该模型继承(您应该这样做) - 那么电子邮件将不会被提示,也不会传递给 create_superuser 方法.
Looking at the code for the management commands, it only prompts for fields in the user model's REQUIRED_FIELDS
attribute (as well as username). That attribute contains email by default in AbstractBaseUser, but if you have overridden it - or not inherited from that model in the first place (which you should be doing) - then email will not be prompted, and not passed to the create_superuser method.
这篇关于实现自定义用户模型时 createsuperuser 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!