1

I complete makemigrations and migrate then create a superuser. After this http://127.0.0.1:8000/admin i try to log in then show this error Please enter the correct Email and password for a staff account. Note that both fields may be case-sensitive

seetings.py

AUTH_USER_MODEL = 'user.CustomUser'


**models.py**
from django.db import models
from django.contrib.auth.models import AbstractUser,BaseUserManager


class CustomUserManager(BaseUserManager):
    """
    Creates and saves a User with the given email, date of
    birth and password.
    """
    def create_user(self, email, password=None):
        if not email:
            raise ValueError('User must have an email id')

        user=self.model(
            email=self.normalize_email(email)
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password=None):
        user=self.create_user(
            email,
            password=password
        )
        user.is_admin = True
        user.save(using=self._db)
        return user


class CustomUser(AbstractUser):
    #only add email field for login

    email = models.EmailField(
        verbose_name='Email',
        max_length=50,
        unique=True
    )
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = CustomUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def __str__(self):
        return self.email



***admin.py***

from django.contrib import admin
from django.contrib.auth import get_user_model

user=get_user_model()

admin.site.register(user)
  • Are you sure re-create method `create_superuser`? Maybe password must be `make_password(password)`. Example like `def create_superuser` in `UserManager`. Here is django code: https://github.com/django/django/blob/main/django/contrib/auth/models.py – Mahrus Khomaini Sep 22 '21 at 08:12
  • In method `def _create_user(self, username, email, password, **extra_fields)` – Mahrus Khomaini Sep 22 '21 at 08:14
  • The `password` must create using `make_password` function. `from django.contrib.auth.hashers import make_password` – Mahrus Khomaini Sep 22 '21 at 08:15
  • @MahrusKhomaini Thank you for your informative comment. Now the problem is solved – Motalib Hossain Sep 22 '21 at 09:38

1 Answers1

0

i had this problem. you should define is_staff=True in create_superuser function and you should pass **kwargs as argument in this function. i hope your problem solve.

ahnaimi
  • 21
  • 3