imm trying to login to the django admin panel with a successfully created superuser but cannot get the right username/pw combo right.
I want users to just use their email as their username. I've deleted migrations, sycndb, and everything works except logging in to the admin panel.
models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
class MyAccountManager(BaseUserManager):
def create_user(self, email, username, password= None):
if not email:
raise ValueError('email required')
if not username:
raise ValueError('username required')
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):
user = self.create_user(
email= self.normalize_email(email),
password= password,
username= username,
)
user.is_admin= True
user.staff= True
user.is_superuser= True
user.save(using= self._db)
return user
class Accounts(AbstractBaseUser):
email = models.EmailField(verbose_name= 'email', max_length= 60, unique= True)
username = models.CharField(max_length= 30, unique= True)
date_joined = models.DateTimeField(verbose_name= 'date joined', auto_now_add= True)
last_login = models.DateTimeField(verbose_name= 'last login', auto_now= True)
is_admin = models.BooleanField(default= False)
is_active = models.BooleanField(default= True)
is_staff = models.BooleanField(default= False)
is_superuser = models.BooleanField(default= False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
objects = MyAccountManager()
def __str__(self):
return self.email
def has_perm(self, perm, obj= None):
return self.is_admin
def has_module_perm(self, app_label):
return True
admin.py
from django.contrib import admin
from .models import Accounts
admin.site.register(Accounts)
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'articles',
'accounts',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
]
}
AUTH_USER_MODEL = 'accounts.Accounts'
ROOT_URLCONF = 'restApi.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
I think it must be something to do with the way the password is being saved and returned, because no matter what I do I get the "Please enter the correct email and password for a staff account. Note that both fields may be case-sensitive." message.
imm using mongodb database.
You're setting user.staff= True
in MyAccountManager.create_superuser()
, but it should be user.is_staff = True
(you're missing is_
).