0

我完成了 makemigrations 并迁移,然后创建了一个超级用户。在此 http://127.0.0.1:8000/admin之后,我尝试登录然后显示此错误 请输入正确的员工帐户的电子邮件和密码。两个地方都要注意大小写

设置.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)
4

0 回答 0