custom django-user object has no attribute 'has_module_perms'

后端 未结 3 1019
刺人心
刺人心 2021-02-12 07:00

My custom user model for login via email:

class MyUser(AbstractBaseUser):
    id = models.AutoField(primary_key=True)  # AutoField?
    is_superuser = models.Int         


        
3条回答
  •  醉梦人生
    2021-02-12 07:51

    For those who are just stepping in November 2017 and after, I don't think adding or allowing your class to inherit PermissionsMixin is the way out, being that it will raise more error since you have reinvented the wheel.

    I ran into the same problem this afternoon (4th Nov, 2017) having override Username with Phone Number:

    class MyUserManager(BaseUserManager):
        ..
        ..
    
        def create_superuser(self, phone, password=None):
            if password is None:
                raise TypeError("provide password please")
            myuser = self.model(phone=phone)
            myuser.set_password(password)
            myuser.is_admin = True
            myuser.is_staff = True
            myuser.save()
    
            return myuser
    

    So, http://127.0.0.1:8000/admin/ wasn't working and kept raising object has no attribute 'has_module_perms' error, the following is how I fixed mine:

    class MyUser(AbstractBaseUser):
        ..
        ..
    
        def get_full_name(self):
            pass
    
        def get_short_name(self):
            pass
    
        @property
        def is_superuser(self):
            return self.is_admin
    
        @property
        def is_staff(self):
           return self.is_admin
    
        def has_perm(self, perm, obj=None):
           return self.is_admin
    
        def has_module_perms(self, app_label):
           return self.is_admin
    
        @is_staff.setter
        def is_staff(self, value):
            self._is_staff = value
    

    I hope this helps someone.

提交回复
热议问题