Django - create_superuser()得到了一个意外的关键字参数'user_type'

时间:2017-11-18 19:05:58

标签: python django django-models

我正在尝试为我的django应用创建自定义用户模型。 以下是CustomUser和CustomUserManager的代码。

from django.utils.translation import ugettext_lazy as _

class CustomUserManager(BaseUserManager):
def _create_user(self, anonymous, first_name, last_name, email, username, password, home_address, user_type, image_path):
    now = timezone.now()

    if not email:
        raise ValueError('The gives email must be set')

    email = self.normalize_email(email)
    user = self.model(
        anonymous=anonymous,
        first_name=first_name,
        last_name=last_name,
        email=email,
        username=username,
        home_address=home_address,
        user_type=user_type,
        image_path=image_path,
        created_time=now,
        last_login=now
    )

    user.set_password(password)
    user.save(using=self._db)
    return user


    def create_superuser(self, first_name, last_name, email, username, password, home_address, image_path):
    return self._create_user(False, first_name, last_name, email, username, password, home_address, 0, image_path)

class CustomUser(AbstractBaseUser):
    anonymous = models.BooleanField()
    username = models.CharField(max_length=255, unique=True)
    first_name = models.CharField(max_length=255, blank=True)
    last_name = models.CharField(max_length=255, blank=True)
    email = models.EmailField(blank=True, unique=True)
    home_address = models.CharField(max_length=255, blank=True)
    user_type = models.IntegerField(1)
    image_path = models.CharField(max_length=500, blank=True)
    created_time = models.TimeField()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username', 'home_address', 'first_name', 'last_name', 'user_type']

    objects = CustomUserManager()
    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

然后我收到有关意外参数user_type

的错误
  

追踪(最近一次呼叫最后一次):

     

文件“manage.py”,第22行,in       execute_from_command_line(sys.argv中)

     

文件“C:\ Users \ Nutzer \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ core \ management__init __。py”,第363行,在execute_中   from_command_line       utility.execute()

     

文件“C:\ Users \ Nutzer \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ core \ management__init __。py”,第355行,执行中       self.fetch_command(子命令).run_from_argv(self.argv)

     

文件“C:\ Users \ Nutzer \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ core \ management \ base.py”,第283行,在run_from_arg中   v       self.execute(* args,** cmd_options)

     

文件“C:\ Users \ Nutzer \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ contrib \ auth \ management \ commands \ createsuperuser.py   “,第63行,执行中       return super(命令,self).execute(* args,** options)

     

文件“C:\ Users \ Nutzer \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ core \ management \ base.py”,第330行,执行中       output = self.handle(* args,** options)

     

文件“C:\ Users \ Nutzer \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ django \ contrib \ auth \ management \ commands \ createsuperuser.py   “,第183行,处理       self.UserModel._default_manager.db_manager(数据库).create_superuser(**的user_data)

     

TypeError:create_superuser()得到了一个意外的关键字参数'user_type'

如果有人可以提供帮助,请提前感谢!

1 个答案:

答案 0 :(得分:1)

我没有看到您将自定义管理器分配到自定义模型的位置。这表明它似乎在查看堆栈跟踪中的默认管理器。尝试将自定义管理器分配给模型as described in the docs