create_superuser用于自定义身份验证模型

时间:2017-02-26 20:51:09

标签: python django

我创建了以下代码来构建自定义Django身份验证模型,我尝试使用自定义用户管理器创建超级用户,但是当我尝试使用python manage.py createsuperuser命令时,收到以下错误:{ {1}}

UserHandler / models.py

TypeError: create_superuser() got an unexpected keyword argument 'password'

Settings.py

# Custom Auth Manager
class AuthManager(BaseUserManager):
    def create_superuser(self, supplied_email, supplied_password, supplied_first_name, supplied_last_name, supplied_language):
        # Make this password great again (w/ salting and hashing)
        hashed_salted_password = AbstractBaseUser.set_password(supplied_password)
        superuser = AuthLookup()
        superuser.email_address = supplied_email
        superuser.password = hashed_salted_password
        superuser.first_name = supplied_first_name
        superuser.last_name = supplied_last_name
        superuser.language = supplied_language
        superuser.save()


# Abstract Auth Model
class AuthLookup(AbstractBaseUser, PermissionsMixin):
    email_address = models.EmailField(unique=True)
    password = models.CharField(max_length=100)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    organization_id = models.UUIDField(null=False, max_length=36, default=uuid.uuid4)
    # Firebase tokens(Max length represents 5 device tokens at maximum length of 256 characters plus 4 commas.)
    associated_devices = models.CharField(max_length=1284)
    is_requester = models.BooleanField(null=False, default=0)
    is_responder = models.BooleanField(null=False, default=0)
    user_identifier = models.UUIDField(null=False, max_length=36, default=uuid.uuid4)
    language = models.CharField(max_length=6, default='US_EN')
    user_added = models.DateTimeField(default='1970-01-01 00:00:00')

    object = AuthManager()

    USERNAME_FIELD = 'email_address'
    REQUIRED_FIELDS = ['first_name', 'last_name', 'language']

1 个答案:

答案 0 :(得分:1)

事实证明,传递给create_superuser函数的参数必须与模型中的字段命名相同。

现在不存在错误。 (不幸的是,我转向了不同的错误)

class AuthManager(BaseUserManager):
def create_superuser(self, email_address, password, first_name, last_name, language):
    # Keep everyone honest here, make sure the email is valid
    email_address = self.normalize_email(email_address)
    # Make this password great again (w/ salting and hashing)
    password = AbstractBaseUser.set_password(password)
    superuser = AuthLookup()
    superuser.email_address = email
    superuser.password = password
    superuser.first_name = first_name
    superuser.last_name = last_name
    superuser.language = language
    superuser.save(using=self._db)