Home > Software engineering >  Create a custom superuser django
Create a custom superuser django

Time:03-07

I'm trying to create a custom user model but when I create a superuser with "python manage.py createsuperuser" and try to login from the django admin, I get this message:

Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive.

username and password are correct.

and this is my models:

class MyUserManager(BaseUserManager):
    def create_user(self, username, password, **extra_fields):
        user = self.model(username=username)
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, username, password, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_active', True)
    
        return self.create_user(username, password, **extra_fields)


class User(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(max_length=10,unique=True)

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = []


    publisher = models.BooleanField(default=False)
    admin = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)

    objects = MyUserManager()

    def __str__(self):
        return self.username

CodePudding user response:

You can try including django.contrib.auth.backends.ModelBackend in the AUTHENTICATION_BACKENDS parameter. Similar question is available here.

CodePudding user response:

You must include your extra_fields when creating your user. So your third line must be like below:

user = self.model(username=username, **extra_fields)
  • Related