I have already created a custom user model for profiling and Authentication, however it doesn't seem to authenticate the password. I tried numerous times on changing it and even confirmed it on shell but it still doesn't go through on Django Admin.
Here is my models.py:
from django.db import models
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser
)
# Create your models here.
class UserManager(BaseUserManager):
def create_user(self, email, password=None, user_type=None, is_superuser=False, is_pqa=False, is_staff=False, is_admin=False, is_test_lead=False, is_test_manager=False, is_active=True):
if not email:
raise ValueError("User must have an email address!")
if not password:
raise ValueError("User must have a password!")
user_obj = self.model(
email=self.normalize_email(email)
)
user_obj.set_password(password)
user_obj.user_type = user_type
user_obj.ad = is_admin
user_obj.superuser = is_superuser
user_obj.tm = is_test_manager
user_obj.pqa = is_pqa
user_obj.tl = is_test_lead
user_obj.active = is_active
user_obj.save(using=self._db)
return user_obj
def create_pqa(self, email, password=None):
user = self.create_user(
email,
password=password,
is_pqa=True,
is_staff=True,
user_type=1
)
return user
def create_staff(self, email, password=None):
user = self.create_user(
email,
password=password,
is_staff=True,
user_type=2
)
return user
def create_test_lead(self, email, password=None):
user = self.create_user(
email,
password=password,
is_test_lead=True,
is_staff=True,
user_type=3
)
return user
def create_test_manager(self, email, password=None):
user = self.create_user(
email,
password=password,
is_test_manager=True,
is_staff=True,
user_type=4
)
return user
def create_superuser(self, email, password=None):
user = self.create_user(
email,
password=password,
is_admin=True,
is_superuser=True,
is_staff=True,
user_type=5
)
return user
class User(AbstractBaseUser):
USER_TYPE_CHOICES = (
(1, 'pqa'),
(2, 'tester'),
(3, 'test_lead'),
(4, 'test_manager'),
(5, 'admin'),
)
email = models.EmailField(max_length=255, unique=True)
full_name = models.CharField(max_length=255, blank=True, null=True)
active = models.BooleanField(default=True)
birth_date = models.DateField(null=True, blank=True)
hire_date = models.DateField(null=True, blank=True)
user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES)
ad = models.BooleanField(default=False) # superuser/admin
# non-superuser but with Team Manager Privilages
tm = models.BooleanField(default=False)
# non-superuser but with PQA Privilages
pqa = models.BooleanField(default=False)
ts = models.BooleanField(default=False) # non-superuser
# non-superuser but with TL Privilages
tl = models.BooleanField(default=False)
superuser = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
def __str__(self):
return self.full_name
def get_full_name(self):
return self.full_name
def get_short_name(self):
return self.full_name
@property
def is_pqa(self):
return self.pqa
@property
def is_staff(self):
return self.ts
@property
def is_test_lead(self):
return self.tl
@property
def is_test_manager(self):
return self.tm
@property
def is_admin(self):
return self.ad
@property
def is_active(self):
return self.active
@property
def is_superuser(self):
return self.superuser
And here is my settings.py:
"""
Django settings for ceres project.
Generated by 'django-admin startproject' using Django 3.0.3.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '@3xm&$=ky!sq2_i$-9cd%48ork1l$1y-+e__g_4d!+3*2ssli-'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'profiles.apps.ProfilesConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
AUTH_USER_MODEL = 'profiles.User' # Custom model
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',
]
ROOT_URLCONF = 'ceres.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',
],
},
},
]
WSGI_APPLICATION = 'ceres.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
I really would appreciate if someone would point me at the right direction for this, since I'm at a loss. Thanks in advance!
Create a new superuser by
python manage.py createsuperuser
you will be able to login
Welcome to StackOverFlow!
Possible Steps:
1) Check your superuser from command line. Mozilla MDN Docs are an incredible resource: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Admin_site (Go to "Creating a SuperUser" heading)
2) Maybe it's a configuration thing?
Check out this article by testdriven.io. They do go over creating a custom User Model from the beginning but I think you'll find it helpful even though they're working with email and usernames as they go through all the necessary configurations to get the customizations to show correctly (their "Conclusion" part looks amazing!).
You might find it more helpful to skip to "User Model" since it talks about the AbstractUser right after.
3) Again, Django docs are great! https://docs.djangoproject.com/en/3.0/topics/auth/customizing/
I got similar issue , my solution is add lines below in my settings.py :
SESSION_COOKIE_DOMAIN = 'localhost' ==> or whatever domain for your website (I am running it locally)
Try and see if it works for you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.