0

I am trying to create a superuser with email and password only but I am getting the above error.

After running py manage.py createsuperuser , it asks for admin and password only and I provide respective fields but after bypassing password validation with yes, it gives above error.

class CustomUserManager(BaseUserManager):
    """
    Custom user model manager where email is the unique identifiers
    for authentication instead of usernames.
    """
    def create_user(self,  first_name, last_name, email, password, **extra_fields):
        """
        Create and save a User with the given email and password.
        """
        if not email:
            raise ValueError("The email must be set")
        first_name = first_name.capitalize()
        last_name = last_name.capitalize()
        email = self.normalize_email(email)

        user = self.model(
            first_name=first_name, last_name=last_name, email=email, **extra_fields
        )
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, password, **extra_fields):
        """
        Create and save a SuperUser with the given email and password.
        """
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_active', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError(_('Superuser must have is_staff=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)


class CustomUser(AbstractUser):
    username = None
    #email = models.EmailField(_('email address'), unique=True)
    email = models.EmailField(unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = CustomUserManager()

    def __str__(self):
        return self.email
Reactoo
  • 916
  • 2
  • 12
  • 40

1 Answers1

0

You're passing email and password as a positional arguments to the create_user method you've created, but it takes 4 positional arguments. Arguments that you've provided are being passed into the first_name and last_name instead of email and password. Simply pass something into those fields or make them not required.

GwynBleidD
  • 20,081
  • 5
  • 46
  • 77