| 1 |
--- |
| 2 |
title: Group Model |
| 3 |
description: Using a custom group model with django-guardian. |
| 4 |
--- |
| 5 |
|
| 6 |
# Custom Group Model |
| 7 |
|
| 8 |
!!! note "Added in 2.6" |
| 9 |
|
| 10 |
Django does not provide the same ability to create a custom `auth.Group` |
| 11 |
model as the `AbstractUser` model and probably never will: |
| 12 |
<https://code.djangoproject.com/ticket/29748>. |
| 13 |
|
| 14 |
To use guardian with a custom group model as it is done with a |
| 15 |
custom user model (see `custom-user-model`), a custom `GroupObjectPermission` model can be used. |
| 16 |
|
| 17 |
Here is an example of using a custom group model with guardian: |
| 18 |
|
| 19 |
``` python |
| 20 |
from django.contrib.auth.models import PermissionsMixin |
| 21 |
|
| 22 |
class CustomGroupPermissionsMixin(PermissionsMixin): |
| 23 |
groups = models.ManyToManyField( |
| 24 |
"CustomGroup", |
| 25 |
verbose_name=_("groups"), |
| 26 |
blank=True, |
| 27 |
help_text=_( |
| 28 |
"The groups this user belongs to. A user will get all permissions " |
| 29 |
"granted to each of their groups." |
| 30 |
), |
| 31 |
related_name="user_set", |
| 32 |
related_query_name="user", |
| 33 |
) |
| 34 |
|
| 35 |
class Meta(PermissionsMixin.Meta): |
| 36 |
abstract = True |
| 37 |
``` |
| 38 |
|
| 39 |
This mixin will be used to override the `groups` field of the custom |
| 40 |
user model with a field that uses the custom group model. It can be |
| 41 |
placed in `myapp/mixins.py`. |
| 42 |
|
| 43 |
Then, the custom user model and the custom group model can be defined as |
| 44 |
follows: |
| 45 |
|
| 46 |
``` python |
| 47 |
from .mixins import CustomGroupPermissionsMixin |
| 48 |
from guardian.mixins import GuardianUserMixin, GuardianGroupMixin |
| 49 |
|
| 50 |
class CustomGroup(Group, GuardianGroupMixin): |
| 51 |
foo = models.CharField(max_length=120) # adds a custom field to the original Group model |
| 52 |
|
| 53 |
|
| 54 |
class User(AbstractBaseUser, CustomGroupPermissionsMixin, GuardianUserMixin): |
| 55 |
email = models.EmailField( |
| 56 |
verbose_name=_("email address"), max_length=200, unique=True, db_index=True |
| 57 |
) |
| 58 |
... |
| 59 |
|
| 60 |
|
| 61 |
class CustomGroupObjectPermission(GroupObjectPermissionAbstract): |
| 62 |
group = models.ForeignKey(CustomGroup, on_delete=models.CASCADE) |
| 63 |
|
| 64 |
class Meta(GroupObjectPermissionAbstract.Meta): |
| 65 |
abstract = False |
| 66 |
``` |
| 67 |
|
| 68 |
These models can be placed in `myapp/models.py`. The |
| 69 |
`CustomGroupObjectPermission` will use the custom group model instead of |
| 70 |
the default `auth.Group` model. |
| 71 |
|
| 72 |
To access the `CustomGroupObjectPermission` model, the function |
| 73 |
`get_group_obj_perms_model` can be used: |
| 74 |
|
| 75 |
``` python |
| 76 |
from guardian.utils import get_group_obj_perms_model |
| 77 |
|
| 78 |
GroupObjectPermission = get_group_obj_perms_model() # this returns the correct group permission model |
| 79 |
|
| 80 |
def return_all_group_obj_perms(): |
| 81 |
return GroupObjectPermission.objects.all() |
| 82 |
``` |
| 83 |
|
| 84 |
!!! danger "Important" |
| 85 |
|
| 86 |
When using an custom `GroupObjectPermission` model, do not import the |
| 87 |
`GroupObjectPermission` model directly |
| 88 |
in your code, always use the `get_group_obj_perms_model` function. |
| 89 |
|
| 90 |
By default, django-guardian monkey patches the group model to add some necessary functionality. |
| 91 |
This can result in errors if guardian is imported |
| 92 |
into the `models.py` of the same app where the custom group model lives. |
| 93 |
|
| 94 |
To fix this, it is recommended to add the setting |
| 95 |
`GUARDIAN_MONKEY_PATCH_GROUP = False` in your `settings.py` and subclass |
| 96 |
`guardian.mixins.GuardianGroupMixin` in your custom group model. |
| 97 |
|
| 98 |
The `settings.py` file for the example above would look like this: |
| 99 |
|
| 100 |
``` python |
| 101 |
... |
| 102 |
GUARDIAN_MONKEY_PATCH_USER = False |
| 103 |
GUARDIAN_MONKEY_PATCH_GROUP = False |
| 104 |
GUARDIAN_GROUP_OBJ_PERMS_MODEL = 'myapp.CustomGroupObjectPermission' |
| 105 |
... |
| 106 |
``` |