'Django : can't login in django administration
First of all, I've created a User model, which inherits from AbstractUser. I use another model which is called CustomUserManager (which inherits from UserManager). In CustomUserManager, i rewrote the create_user method, and the create_superuser method. When i create a simple active user, it works fine (i can connect the user). When i create a superuser, it works fine ("Superuser created successfully") BUT when i go to the django administration pannel, and i enter the correct information of the superuser, it doesn't work and display the following message : "Please enter the correct email and password for a staff account. Note that both fields may be case-sensitive. "
Here's my models.py :
class CustomUserManager(UserManager):
def _create_user(self,email,password,first_name,last_name,**extra_fields):
email = self.normalize_email(email)
user = User(email=email)
user.is_active=True
user.password = make_password(password)
user.save(using=self._db)
return user
def create_user(self,email,password,first_name,last_name,**extra_fields):
return self._create_user(email,password,first_name,last_name,**extra_fields)
def create_superuser(self,email,password,first_name,last_name,**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,first_name, last_name, **extra_fields)
class User(AbstractUser):
username = None
email = models.EmailField(unique=True, blank=False)
first_name = models.CharField(blank=False,max_length=50)
last_name = models.CharField(blank=False,max_length=50)
REQUIRED_FIELDS=["first_name","last_name"]
USERNAME_FIELD = 'email'
objects = CustomUserManager()
EDIT : Thanks to Max Koniev, I forgot to say, but i already had AUTH_USER_MODEL set to my User model in my application private_portal : AUTH_USER_MODEL = 'private_portal.User'
Solution 1:[1]
Be sure you set AUTH_USER_MODEL in settings.py to your new User model
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Max Koniev |
