Home > Net >  Unable to login to Django admin with Custom superuser
Unable to login to Django admin with Custom superuser

Time:09-18

I am developing a school management system where I am creating custom user by extending User model using AbstractUser. I am able to create the superuser using custom user model but whenever I am trying to login using custom superuser account Django gives the following error

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

This is my customUser model

class CustomUser(AbstractUser):
ADMIN = "1"
STAFF = "2"
STUDENT = "3"

Email_to_User_Type_Map = {
    'admin': ADMIN,
    'staff': STAFF,
    'student': STUDENT
}
user_type_data = ((ADMIN, 'Admin'), (STAFF, 'Staff'), (STUDENT, 'Student'))
user_type = models.CharField(
    default=1, choices=user_type_data, max_length=10)

Below function represents creating custom user function

def create_customUser(username, email, password, email_status):
new_user = CustomUser()
new_user.username = username
new_user.email = email
new_user.password = password
if email_status == "staff" or email_status == "student":
    new_user.is_staff = True
elif email_status == "admin":
    new_user.is_staff = True
    new_user.is_superuser = True

new_user.is_active = True
new_user.save()

please help

CodePudding user response:

The best practice is to create new User using a serializer or form

CodePudding user response:

Sorry guys but I found the answer after getting some hint from @Mukhtor answer. What I am doing is creating the instance of CustomUser model which will create the superuser but not generating the hash password, so I change my code little bit and its working fine now.

Modified create_customer function

def create_customUser(username, email, password, email_status):
new_user = CustomUser.objects.create_user(
    username=username, email=email, password=password)
if email_status == "staff" or email_status == "student":
    new_user.is_staff = True
elif email_status == "admin":
    new_user.is_staff = True
    new_user.is_superuser = True

new_user.is_active = True
new_user.save()

return
  • Related