简体   繁体   中英

Unable to login to Django admin with Custom superuser

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

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

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM