Unable to create superuser after creating CustomUser

After creating Custom user when i tried to create a superuser it gave error

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

Then after creating CustomUserManager its showing

Expression of type "CustomUserManager" is incompatible with declared type "UserManager[Self@AbstractUser]" "CustomUserManager" is incompatible with "UserManager[Self@AbstractUser]"

Expression of type "CustomUserManager" is incompatible with declared type "UserManager[Self@AbstractUser]"
  "CustomUserManager" is incompatible with "UserManager[Self@AbstractUser]"

class CustomUserManager(BaseUserManager):
    def create_user(self, email, password=None, **extra_fields):
        if not email:
            raise ValueError('The Email field 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_superuser(self, email, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        return self.create_user(email, password, **extra_fields)


class CustomUser(AbstractUser, PermissionsMixin):
    username = None
    email = models.EmailField(unique=True)  

    daily_reminders = models.IntegerField(default=0)
    start_at = models.TimeField(null=True, blank=True)
    end_at = models.TimeField(null=True, blank=True)
    notification_status = models.IntegerField(null=True, blank=True)
    device_type = models.CharField(choices=DIV_CHOICES, max_length=10, null=True, blank=True)
    device_token = models.CharField(max_length=255, null=True, blank=True)
    categories = models.JSONField(default=list, null=True, blank=True)
    

    USERNAME_FIELD = 'email'  
    REQUIRED_FIELDS = []  
    
    objects = CustomUserManager()

    def __str__(self):
        return self.email

I BaseUserManager imported

from django.contrib.auth.base_user import BaseUserManager

but still showing error

Its been a very long time since I done any Django but I do remember that when it comes from custom user classes -- the models had to be updated before the very first migration as otherwise things would not work as expected. Not sure if this is still the case or even if I am answering your question :)

There is no problem with the code.

However, the database migration probably has UserManager applied.

Makemigrations and migrate process are also required when you change the Manager.

python3 manage.py makemigrations YourUserApp
python3 manage.py migrate

CustomUserManager has not yet been applied, Please try again after doing the migrations!

Вернуться на верх