[Python] i got TypeError: UserManager.create_superuser() missing 1 required positional...

Discussão em 'Python' iniciado por Stack, Outubro 24, 2024.

  1. Stack

    Stack Membro Participativo

    I want to create super user just from email and password so it should create super user without username. I am using custom user model and when i try to create superuser i got this error TypeError: UserManager.create_superuser() missing 1 required positional argument: 'username'

    This is my manager.py file

    from django.contrib.auth.base_user import BaseUserManager


    class UserManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self, email, password, **extra_fields):

    if not email:
    raise ValueError('The given email must be set')
    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=None, **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)
    extra_fields.setdefault('is_staff', True)

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

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


    This is my models.py

    class User(AbstractUser)
    """
    Abstract User of django auth user model.
    """
    uuid = models.UUIDField(default=uuid.uuid4, unique=True, null=False)
    email = models.EmailField(_("email address"), unique=True)
    middle_name = models.CharField(max_length=120, blank=True)
    image = models.ImageField(upload_to='user_profile/', blank=True)
    department = models.CharField(blank=True, choices=Department.choices,
    max_length=2)
    designation = models.CharField(max_length=120, blank=True)
    last_logout = models.DateTimeField(null=True)
    is_superuser = models.BooleanField(default=False)
    view = models.TextField(blank=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    Continue reading...

Compartilhe esta Página