无法添加Django超级用户
因此,我创建了一个AbstractBaseUser,当我添加一个超级用户时,我得到一个错误:无法添加Django超级用户,django,django-authentication,Django,Django Authentication,因此,我创建了一个AbstractBaseUser,当我添加一个超级用户时,我得到一个错误: class MyUser(AbstractBaseUser, PermissionsMixin): email = models.EmailField( verbose_name='email address', max_length=255, unique=True, ) donator = models.DateTimeField() USERNAME_F
class MyUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
donator = models.DateTimeField()
USERNAME_FIELD = 'email'
objects = UserManager()
def __str__(self):
return self.email
@property
def is_staff(self):
return self.is_admin
我只是在学习教程,所以我不知道自己在做什么。我在这里看过无数的帖子&文档,但似乎没有一篇适合我。我只是不断得到这个错误:
TypeError: create_superuser() takes exactly 4 arguments (3 given)
有什么想法吗 这是我拥有的MyUserManager类,MyUser类现在也指向这一点
class MyUserManager(BaseUserManager):
def create_user(self, email, date_of_birth, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
date_of_birth=date_of_birth,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, date_of_birth, password):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
user = self.create_user(email,
password=password,
date_of_birth=date_of_birth
)
user.is_admin = True
user.save(using=self._db)
return UserWarning
我只是直接从Django文档中删除了这个,所以我不认为这是问题所在,或者说是吗?问题确实出在经理身上。您已经重写了create\u superuser方法,以获取一个附加参数date\u of\u birth。但是Django createsuperuser命令对此一无所知,所以它只传递正常的三个参数 现在,看起来用户模型的REQUIRED_FIELDS属性中的任何内容都将被传递到创建_superuser 事实上,看看这本书,有一行你错过了:
REQUIRED_FIELDS = ['date_of_birth']
这段代码与您的错误有什么关系?您在哪里调用了create_superuser?我刚刚执行了python manage.py createsuperuser,之后什么时候会出现此错误?马上?什么是用户管理器?那是你自己的班级吗?如果是这样的话,你应该发布它。我在输入两个密码后都会得到错误。我刚刚试着运行服务器,但遇到一个错误,说Manager不可用;用户已被替换为“accounts.MyUser”:我已将我的经理重命名为MyUserManager,以防发生任何冲突,但它不起作用。这应该是对问题的编辑,而不是作为答案发布。