can not login with Extended AbstractBaseUser model

心不动则不痛 提交于 2019-12-25 10:05:09

问题


I'm using Django 2.0

I have extended the AbstractBaseUser to make some modification to the email and use email as username

class UserManager(BaseUserManager):
    def create_user(self, email, password=None, is_staff=False, is_superuser=False):
        if not email:
            raise ValueError('User must have an email address')
        if not password:
            raise ValueError('User must have a password')

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

    def create_staffuser(self, email, password=None):
        return self.create_user(
            email,
            password=password,
            is_staff=True
        )

    def create_superuser(self, email, password=None):
        return self.create_user(
            email,
            password=password,
            is_staff=True,
            is_superuser=True
        )


class User(AbstractBaseUser):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    email = models.EmailField(max_length=250, blank=False, unique=True)
    first_name = models.CharField(max_length=150, blank=True)
    last_name = models.CharField(max_length=150, blank=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    groups = models.ManyToManyField(Group, blank=True)
    last_login = models.DateTimeField(auto_now=True)
    date_joined = models.DateTimeField(auto_now_add=True)

    USERNAME_FIELD = 'email'

    objects = UserManager()

    @property
    def staff(self):
        return self.is_staff

    @property
    def active(self):
        return self.is_active

    @property
    def superuser(self):
        return self.is_superuser

    def __str__(self):
        if self.first_name is not None:
            return self.get_full_name()

        return self.email

    def get_full_name(self):
        if self.last_name is not None:
            return self.first_name + ' ' + self.last_name

        return self.get_short_name()

    def get_short_name(self):
        return self.first_name

I have then run

python manage.py makemigrations
python manage.py migrate

and created superadmin using

python manage.py createsuperuser

The superuser is created successfully.

But when I login using 127.0.0.1:8000/admin/

I gives

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

What is wrong with the code?


回答1:


You need to define has_perm and has_module_perms methods:

def has_perm(self, perm, obj=None):
    "Does the user have a specific permission?"
    # Simplest possible answer: Yes, always
    return True

def has_module_perms(self, app_label):
    "Does the user have permissions to view the app `app_label`?"
    # Simplest possible answer: Yes, always
    return True


来源:https://stackoverflow.com/questions/48070557/can-not-login-with-extended-abstractbaseuser-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!