Home > front end >  Create super user in Django with email instead of username
Create super user in Django with email instead of username

Time:12-26

This is the first time that I ask something... So I'll try to be precise.

I am building a django app for my final project, and I have encountered some trouble with the authentication system.

What I'm trying to do is create a superuser with the command:

python manage.py createsuperuser

But I want to use the email adress, instead of the username field.

For this reason, I wrote the following code:

My CustomUser:

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

And I also created a CustomUserManager (I thought that, by doing that, I wouldn't encounter any more problems):

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)

But now, when I run the command to create the superuser, I get this error message:

TypeError: UserManager.create_superuser() missing 1 required positional argument: 'username'

I also created the corresponding variable in my settings.py with the custom user:

AUTH_USER_MODEL = 'users_auth.CustomUser'

So I don't know that the problem could be... I would appreaciate any help you could offer.

Thanks!

CodePudding user response:

Try this code

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 = []

CodePudding user response:

Refer this link Here clearly explains how custom superuser can be created.

  • Related