I need to use email for authentication instead of user but when ever I create super user it doesn't set the is_active and is_admin to True which are False by default!
models.py
class CustomUserManager(BaseUserManager):
def create_user(self, email, username, password=None):
if not email:
raise ValueError('User must have an email')
if not password:
raise ValueError("User must have a password")
user = self.model(
email = self.normalize_email(email),
username = username,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, username, password=None):
user = self.create_user(email, username, password)
user.is_active = True
user.is_staff = True
user.is_admin = True
user.is_superuser = True
user.save(using=self._db)
return user
class User(AbstractUser):
email = models.EmailField(max_length=200,unique=True)
username = models.CharField(max_length=200, unique=True)
last_login = models.DateTimeField(auto_now=True)
created_at = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
def __str__(self):
return self.email
objects = CustomUserManager
setting
AUTH_USER_MODEL = "account.User"
SQL table for users
id ... is_active is_staff is_admin is_superuser
1 ... 0 1 0 1
2 ... 0 1 0 1
3 ... 0 1 0 1
4 ... 0 1 0 1
You are still using the old manager (the _base_manager
), you should set the new manager by creating a manager object:
class User(AbstractUser):
# …
objects = CustomUserManager() # 🖘 use parenthesis
This will then also set User._default_manager
to this CustomUserManager
object, which is used by the createsuperuser
command. Indeed, in the source codeĀ [GitHub], we see:
self.UserModel._default_manager.db_manager(database).create_superuser( **user_data )