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

时间:2014-06-28 17:41:58

标签: python django django-models root

我正在尝试创建自定义用户模型进行身份验证,但是我无法在代码中看到错误,也许您可​​以看到并帮助我。

相信我在发布之前在整个论坛中搜索,甚至是I read this post,但这是关于哈希密码

当我尝试使用命令

在shell中创建超级用户时
c:\employee>python manage.py createsuperuser

我收到以下错误(底部完成回溯)

create_superuser() got an unexpected keyword argument 'NickName'

这是我的seetings.py

#seetings.py

AUTH_USER_MODEL = 'Sinergia.Employee'

和我的models.py

#models.py
# -*- coding: utf-8 -*-

from django.db import models


# Importando la configuración
from django.conf import settings

# Importando clases para los administradores
# de usuario.
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager

class EmployeeManager(BaseUserManager):
    def create_user(self, email, nickname, password = None):
        if not email:
            raise ValueError('must have an email address.')

        usuario =   self.model\
                    (
                        Email = self.normalize_email(email),
                        NickName = nickname,
                    )
        usuario.set_password(password)
        usuario.save(using = self._db)
        return usuario

    def create_superuser(self, email, nickname, password):
        usuario =   self.create_user\
                    (
                        email = email,
                        nickname = nickname,
                        password = password,

                    )
        usuario.is_admin = True
        usuario.save(using = self._db)
        return usuario


class Employee(AbstractBaseUser):
    Email       = models.EmailField(max_length = 254, unique = True)
    NickName    = models.CharField(max_length = 40, unique = True)
    FBAccount   = models.CharField(max_length = 300)

    # Estados del Usuario.
    is_active   = models.BooleanField(default = True)
    is_admin    = models.BooleanField(default = False)

    object = EmployeeManager()

    # Identificador Único del Usuario.
    USERNAME_FIELD = 'Email'

    # Campos obligatorios.
    REQUIRED_FIELDS = ['NickName']


    def get_full_name(self):
        return self.Email

    def get_short_name(self):
        return self.NickName

    def __unicode__(self):
        return self.Email

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All admins are staff
        return self.is_admin



class Article(models.Model):
    Author       = models.ForeignKey(settings.AUTH_USER_MODEL)

4 个答案:

答案 0 :(得分:2)

分配经理时有错字:

class Employee(AbstractBaseUser):
    ...
    objects = EmployeeManager()

对象,而不是object

答案 1 :(得分:1)

您使用CamelCase作为字段名称,这不是一个好习惯,而且会导致错误。

因此,当您致电(create_superuser())时,您的功能失败:

self.create_user(email = email, nickname = nickname, password = password)

要么致电:

self.create_user(Email = email, NickName = nickname, password = password)

或者,也可以将所有字段名称lowercase

答案 2 :(得分:0)

1-您应该继承 PermissionMixin

在员工中

class Employee(AbstractBaseUser, PermissionsMixin):
     .....

由于 USERNAME_FIELD 存在于 PermissionMixin 类中,因此您可以覆盖它。

2- 对象不是对象

答案 3 :(得分:0)

我的问题解决的是像这样添加**extra_fields

def create_superuser(self, email, password,**extra_fields):
       """
       Creates and saves a superuser with the given email,  and password.
       """
       user = self.create_user(email,
           password=password,**extra_fields
       )
       user.is_admin = True
       user.save()

       return user

也能解决你的问题