在Django中用email代替用户名创建超级用户

jvidinwx  于 2022-12-27  发布在  Go
关注(0)|答案(1)|浏览(109)

这是我第一次问...所以我会尽量精确。
我正在为我的最终项目构建一个django应用程序,我遇到了一些认证系统的问题。
我尝试使用以下命令创建一个超级用户:

python manage.py createsuperuser

但我想使用电子邮件地址,而不是用户名字段。
为此,我编写了以下代码:
我的自定义用户:

class CustomUser(AbstractUser):
    username = models.CharField(max_length=50, null=True, blank=True)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    creation_date = models.DateTimeField(auto_now_add=True)
    modification_date = models.DateTimeField(auto_now=True)

    name = models.CharField(max_length=50, blank=False, null=False)
    last_name = models.CharField(max_length=50, blank=True, null=True)
    email = models.EmailField(unique=True)
    date_of_birth = models.DateField(blank=False, null=False)
    nif = models.CharField(max_length=9, blank=False, null=False)
    sex = models.CharField(max_length=1, choices=SEX_CHOICES)
    phone = models.CharField(max_length=15, blank=True, null=True)
    mobile = models.CharField(max_length=15, blank=True, null=True)

    specialty = models.CharField(max_length=30, choices=SPECIALTY_CHOICES)
    license_number = models.CharField(max_length=50)

    

    def __str__(self):
        string = 'Name: ' + self.name + ' ' + self.last_name + ', email: ' + self.email
        return string
    
    def has_role(self, center, role):
        try:
            center_role = CenterRole.objects.get(user=self, center=center, role=role)
            return True
        except CenterRole.DoesNotExist:
            return False

我还创建了一个CustomUserManager(我认为这样做就不会再遇到任何问题):

class CustomUserManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self, email, password, **extra_fields):
        """
        Creates and saves a CustomUser with the given email and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password, **extra_fields):
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self._create_user(email, password, **extra_fields)

但是现在,当我运行命令创建超级用户时,我收到了以下错误消息:
类型错误:UserManager. create_superuser()缺少1个必需的位置参数:'用户名'
I also created the corresponding variable in my settings.py with the custom user:

AUTH_USER_MODEL = 'users_auth.CustomUser'

所以我不知道问题是什么...我很感激你能提供的任何帮助。
谢谢!

iezvtpos

iezvtpos1#

试试这个代码

class CustomUser(AbstractUser):
    username = models.CharField(max_length=50, null=True, blank=True)
    creation_date = models.DateTimeField(auto_now_add=True)
    modification_date = models.DateTimeField(auto_now=True)
    name = models.CharField(max_length=50, blank=False, null=False)
    last_name = models.CharField(max_length=50, blank=True, null=True)
    email = models.EmailField(unique=True)
    date_of_birth = models.DateField(blank=False, null=False)
    nif = models.CharField(max_length=9, blank=False, null=False)
    sex = models.CharField(max_length=1, choices=SEX_CHOICES)
    phone = models.CharField(max_length=15, blank=True, null=True)
    mobile = models.CharField(max_length=15, blank=True, null=True)
    specialty = models.CharField(max_length=30, choices=SPECIALTY_CHOICES)
    license_number = models.CharField(max_length=50)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

相关问题