0
  • What is the cause of this error? I got this error after changing the admin style 'User' object has no attribute 'get_all_permissions' plz help me. (What is the cause of this error? I got this error after changing the admin style 'User' object has no attribute 'get_all_permissions' plz help me. )

     class UserManager(BaseUserManager):
            def create_user(self, email, username, full_name, phone, password):
                if not email:
                    raise ValueError('plz input email')
                if not username:
                    raise ValueError('plz input username')
                if not full_name:
                    raise ValueError('plz input full_name')
                if not phone:
                    raise ValueError('plz input phone')
    
                user = self.model(email=self.normalize_email(email), username=username, full_name=full_name, phone=phone)
                user.set_password(password)
                user.save(using=self._db)
                return user
    
            def create_superuser(self, email, username, full_name, phone, password):
                user = self.create_user(email, username, full_name, phone, password)
                user.is_admin = True
                user.is_superuser = True
                user.save(using=self._db)
                return user
    
    
        class User(AbstractBaseUser):
            email = models.EmailField(max_length=50, unique=True)
            username = models.CharField(max_length=100, null=True, blank=True, unique=True)
            full_name = models.CharField(max_length=300)
            phone = models.CharField(max_length=15)
            address = models.CharField(max_length=500)
            is_admin = models.BooleanField(default=False)
            is_active = models.BooleanField(default=True)
            is_superuser = models.BooleanField(default=False)
            permission = models.ManyToManyField(Permission, related_name='users')
            USERNAME_FIELD = 'email'
            REQUIRED_FIELDS = ['username', 'full_name', 'phone']
            objects = UserManager()
    
            def __str__(self):
                return self.email
    
            def has_perm(self, perm, obj=None):
                return self.is_superuser
    
            def has_module_perms(self, app_label):
                return self.is_superuser
    
            @property
            def is_staff(self):
                return self.is_admin
    
h_python_1368
  • 93
  • 1
  • 7
  • Please include your traceback. There's no code here that tries to access a `get_all_permissions` attribute of a user. – markwalker_ Dec 02 '20 at 20:24
  • You need to implement a `get_all_permissions` in your `User` model, this looks like: https://github.com/django/django/blob/5fcfe5361e5b8c9738b1ee4c1e9a6f293a7dda40/django/contrib/auth/models.py#L284 – Willem Van Onsem Dec 02 '20 at 20:29
  • @WillemVanOnsem i try to implement something like u sugest, but i given an error, when u have little time i aprecciate some tips : https://stackoverflow.com/questions/70731384/custom-user-return-empty-get-all-permissions?noredirect=1#comment125044013_70731384 – MagicHat Jan 16 '22 at 19:47

2 Answers2

3

Add these codes to your model.py

  1. Import PermissionsMixin from Django contrib Auth

    from django.contrib.auth.models import PermissionsMixin
    
  2. Then change

    class Account(AbstractBaseUser):
    
     to
    
    class Account(AbstractBaseUser, PermissionsMixin):
    

Then it will work fine

1

Instead of returning self.is_superuser, 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

will work I hope. :-) It's been one and half a year.

Märuf
  • 53
  • 1
  • 1
  • 7