# How to refer or access the custom made permissions in has\_perm() and in required\_permissions?

**URL:** <https://forum.djangoproject.com/t/how-to-refer-or-access-the-custom-made-permissions-in-has-perm-and-in-required-permissions/15154>\
**Category:** Using the ORM\
**Created:** [August 1, 2022, 11:48am UTC](https://forum.djangoproject.com/t/how-to-refer-or-access-the-custom-made-permissions-in-has-perm-and-in-required-permissions/15154 "2022-08-01T11:48:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [August 1, 2022, 11:48am UTC](https://forum.djangoproject.com/t/how-to-refer-or-access-the-custom-made-permissions-in-has-perm-and-in-required-permissions/15154/1 "2022-08-01T11:48:01Z")

</div>

Hi there,

I want to know that how do I refer the custom made permissions in the `has_perm()` method of `User` Model and `required_permissions` attribute of `PermissionRequiredMixin` Class?

Let’s say I create the following custom permission:

```auto
content_type = ContentType.objects.get_for_model(User)

Permission.objects.create(
                codename='custom_permission',
                name='Custom Permission',
                content_type=content_type,
            )

```

and suppose my django app where I created this custom permission is named as:

```auto
mycustomapp

```

How will I refer/access this custom permission in `has_perm()` method and `required_permissions` attribute?

I tried following for `has_perm()`

```auto
from django.contrib.auth.models import Permission

user = User.objects.all()[0]
user.user_permissions.add(Permission.objects.get(name="Custom Permission"))
user.save()

user.has_perm("mycustomapp.user.custom_permission")

```

But it resulted in False, instead of True, even though running `user.user_permissions.all()` show the newly added permission

and I tried this for `required_permissions` attribute:

```auto
class CustomView(PermissionRequiredMixin, View):
    required_permission = ["mycustomapp.user.custom_permission"]

```

But it does not work and when I go to this View on front-end I get `Forbidden 403` error, even though the logged in user has this required permission.

Can anyone tell me how do I refer the custom made permission in `has_perm()` and in `required_permissions`. Please tell me while considering all the details exactly as I gave above in example.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 1, 2022, 12:11pm UTC](https://forum.djangoproject.com/t/how-to-refer-or-access-the-custom-made-permissions-in-has-perm-and-in-required-permissions/15154/2 "2022-08-01T12:11:12Z")

</div>

It should just be `mycustomapp.custom_permission`. See [Custom permissions](https://docs.djangoproject.com/en/4.0/topics/auth/customizing/#custom-permissions) and the various examples starting at [Using the Django authentication system | Django documentation | Django](https://docs.djangoproject.com/en/4.0/topics/auth/default/#permission-caching) showing permissions being used.

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [August 1, 2022, 4:06pm UTC](https://forum.djangoproject.com/t/how-to-refer-or-access-the-custom-made-permissions-in-has-perm-and-in-required-permissions/15154/3 "2022-08-01T16:06:14Z")

</div>

@KenWhitesell thanks, it’s solved after I tried what you said. But now I want to add permissions as a bulk from `Permission` Class. For example, I want to do something like this:

```auto
user.user_permissions.add(Permission.objects.filter(name__icontains="workspace"))

```

I tried the above example in python shell, but it gives me the following error:

```auto
Traceback (most recent call last):
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\db\models\fields\ __init__.py", line 1988, in get_prep_value 
    return int(value)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'QuerySet'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\db\models\fields\related_descriptors.py", line 1048, in add 
    self._add_items(
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\db\models\fields\related_descriptors.py", line 1263, in _add_items
    target_ids = self._get_target_ids(target_field_name, objs)
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\db\models\fields\related_descriptors.py", line 1197, in _get_target_ids
    target_ids.add(target_field.get_prep_value(obj))
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\db\models\fields\related.py", line 1132, in get_prep_value  
    return self.target_field.get_prep_value(value)
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\db\models\fields\ __init__.py", line 1990, in get_prep_value 
    raise e. __class__ (
TypeError: Field 'id' expected a number but got <QuerySet [<Permission: accounts | workspace | Can add workspace>, <Permission: accounts | 
workspace | Can Add User To Workspace DataMeister>, <Permission: accounts | workspace | Can Add User To Workspace PricingMeister>, <Permission: accounts | workspace | Can Add User To Workspace TestingWorkspace1>, <Permission: accounts | workspace | Can Remove User From Workspace DataMeister>, <Permission: accounts | workspace | Can Remove User From Workspace PricingMeister>, <Permission: accounts | workspace | Can Remove User From Workspace TestingWorkspace1>, <Permission: accounts | workspace | Can View User From Workspace DataMeister>, <Permission: accounts | workspace | Can View User From Workspace PricingMeister>, <Permission: accounts | workspace | Can View User From Workspace TestingWorkspace1>, <Permission: accounts | workspace | Can change workspace>, <Permission: accounts | workspace | Can delete workspace>, <Permission: accounts | workspace | Can view workspace>]>.

```

How can I accomplish it? How can I add the filtered bulk of permissions from `Permission` class to any user?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 1, 2022, 5:15pm UTC](https://forum.djangoproject.com/t/how-to-refer-or-access-the-custom-made-permissions-in-has-perm-and-in-required-permissions/15154/4 "2022-08-01T17:15:44Z")

</div>

The `add` method expects each object instance to be added to be a separate positional parameter. You can take advantage of the `*args` pattern to pass the list as a list of positional parameters.

You should be able to do something like:  
`user.user_permissions.add(*Permission.objects.filter(name__icontains="workspace"))`

or, if that fails due to some error:  
`user.user_permissions.add(*list(Permission.objects.filter(name__icontains="workspace")))`

(Note: I’m winging this, I haven’t had a chance to actually try it.)

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [August 1, 2022, 5:29pm UTC](https://forum.djangoproject.com/t/how-to-refer-or-access-the-custom-made-permissions-in-has-perm-and-in-required-permissions/15154/5 "2022-08-01T17:29:23Z")

</div>

@KenWhitesell it worked perfectly

> [@KenWhitesell](#):
>
> user.user\_permissions.add(\*Permission.objects.filter(name\_\_icontains=“workspace”))

But, when I tried to print all user permissions with following command:

```auto
user.user_permissions.objects.all()

```

it gave me following result in the form of QuerySet:

```auto
<QuerySet [<Permission: accounts | workspace | Can add workspace>, <Permission: accounts | workspace | Can Add User To Workspace DataMeister>, <Permission: accounts | workspace | Can Add User To Workspace PricingMeister>, <Permission: accounts | workspace | Can Add User To Workspace TestingWorkspace1>, <Permission: accounts | workspace | Can Remove User From Workspace DataMeister>, <Permission: accounts | workspace | Can Remove User From Workspace PricingMeister>, <Permission: accounts | workspace | Can Remove User From Workspace TestingWorkspace1>, 
<Permission: accounts | workspace | Can View User From Workspace DataMeister>, <Permission: accounts | workspace | Can View User From Workspace PricingMeister>, <Permission: accounts | workspace | Can View User From Workspace TestingWorkspace1>, <Permission: accounts | workspace | Can change workspace>, <Permission: accounts | workspace | Can delete workspace>, <Permission: accounts | workspace | Can view workspace>]>

```

I wanted to know that is the result in QuerySet fine? Should not it be a list?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 1, 2022, 5:41pm UTC](https://forum.djangoproject.com/t/how-to-refer-or-access-the-custom-made-permissions-in-has-perm-and-in-required-permissions/15154/6 "2022-08-01T17:41:43Z")

</div>

The [`all` method](https://docs.djangoproject.com/en/4.0/ref/models/querysets/#all) _always_ returns a queryset. It is no different here than it is in any other query.

Side note - Review the [Permissions and authorization](https://docs.djangoproject.com/en/4.0/topics/auth/default/#permissions-and-authorization) docs. The ManyToManyField already is an implicit reference to a manager. (Also see [Related objects reference | Django documentation | Django](https://docs.djangoproject.com/en/4.0/ref/models/relations/#related-objects-reference)) You do not need to reference the `objects` manager in addition to it. The proper reference is `user.user_permissions.all()`, not `user.user_permissions.objects.all()`.
