自定义userprofile后,我无法在Django中创建超级用户。
当我运行manage.py syncdb
时:
TypeError: create_superuser() takes exactly 4 arguments (3 given)
这是我的userprofile模型:
from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser, PermissionsMixin
from django.core.validators import RegexValidator
class AuthUserManager(BaseUserManager):
def create_user(self, username, email, password=None):
if not username:
raise ValueError('User must have username')
if not email:
raise ValueError('User must have email')
user = self.model(username=username, email=email.self.normalize_email(email))
user.is_active = True
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email, password):
user = self.create_user(username=username, email=email, password=password)
user.is_staff = True
user.is_superuser = True
user.save(using=self.db)
return user
class AuthUser(AbstractBaseUser, PermissionsMixin):
alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', message='Only alphanumeric characters are allowed')
username = models.CharField(unique=True, max_length=30, validators=[alphanumeric])
email = models.EmailField(verbose_name='email field', unique=True, max_length=255)
date_joined = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True, null=False)
is_staff = models.BooleanField(default=False, null=False)
objects = AuthUserManager()
USERNAME_FIELD = 'email'
REQUIRES_FIELDS = ['username']
def get_full_name(self):
return '%s %s %s'.format(self.last_name, self.first_name, self.second_name)
def get_short_name(self):
return self.username
def __str__(self):
self.email
据我了解,REQUIRED_FIELDS
存在一些问题,但它包含username
,所以我没有意识到这个错误