1

I want to add a field in my module to give the user some
permissions like is_superuser field. But how can i do this with customize permission?

    class CustomUser(AbstractUser):
        username = models.CharField(max_length=100,unique=True)
        email = models.EmailField(_('email address'), unique=True)
        image = models.ImageField(default='default.jpg', upload_to='profile_pics ')
        date_joined = models.DateTimeField(_('date joined'),default=timezone.now)
        is_active = models.BooleanField(default=True)
        is_admin = models.BooleanField(default=False)
        is_staff = models.BooleanField(default=False)

        USERNAME_FIELD = 'username'
        REQUIRED_FIELDS = ['email']

        objects = CustomUserManager()
1
  • Usually permissions are stored in groups. A user can belong to zero, one or more groups. A user has a permission if it belongs to a group with that permission. Commented Oct 7, 2019 at 8:26

1 Answer 1

1

You can use groups:

from django.contrib.auth.models import Group
...
new_group, _ = Group.objects.get_or_create(name='new_group')
...
user.groups.add(new_group)
...
if user.groups.filter(name=new_group).exists():
    # good to go
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.