实现自定义用户模型时与创建用户的问题 [英] Issue with createsuperuser when implementing custom user model

查看:722
本文介绍了实现自定义用户模型时与创建用户的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 追溯(大部分最近的电话最后):
文件./manage.py,第10行,< module>
execute_from_command_line(sys.argv)
文件/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/__init__.py,第399行,在execute_from_command_line
utility.execute()
文件/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/__init__.py,第392行,执行
self.fetch_command(子命令).run_from_argv(self.argv)
文件/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/ management / base.py,第242行,在run_from_argv
self.execute(* args,** options .__ dict__)
文件/Users/gabriel/.virtualenvs/hang_server/lib/python3.4 /site-packages/django/core/management/base.py,行285,执行
output = self.handle(* args,** options)
文件/ Users / gabriel /。 virtualenvs / hang_server / lib / python3.4 / site-packages / django / contrib / auth / management / commands / creationuperuser.py,第141行,处理
self.UserModel._default _manager.db_manager(数据库).create_superuser(** user_data)
TypeError:create_superuser()缺少1个必需的位置参数:'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)
用户= 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 =没有,** extra_fields):
返回self._create_user(用户名,电子邮件,密码,False,False,
** extra_fields)

def create_superuser
user = self._create_user(username,email,password,True,True,
** extra_fields)
user.is_active = True
user.save(using = self._db)
返回用户

它好像这样会很直接,但是我似乎无法弄清楚为什么我收到这个错误在第一个地方,因为我有在我的create_superuser函数中指定的电子邮件。我已经在线浏览了几个教程,看不出这样做的不同。有人可以解释我在做什么错吗?

解决方案

查看管理命令的代码,它只会提示用户模型的 REQUIRED_FIELDS 属性(以及用户名)。该属性默认在AbstractBaseUser中包含电子邮件,但是如果您已经覆盖了它,或者不是首先从该模型继承(您应该执行此操作),则不会提示电子邮件,而不会传递给create_superuser方法。 / p>

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'

Here is my 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

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?

解决方案

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.

这篇关于实现自定义用户模型时与创建用户的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆