| 1 |
--- |
| 2 |
title: Custom User Model |
| 3 |
description: Using a custom user model with django-guardian. |
| 4 |
--- |
| 5 |
|
| 6 |
# Custom User model |
| 7 |
|
| 8 |
!!! abstract "Added in version 1.1" |
| 9 |
|
| 10 |
Django comes with the ability to customize default `auth.User` model - |
| 11 |
either by subclassing `AbstractUser` or defining very own class. This |
| 12 |
can be very powerful; it must be done with caution, though. Basically, |
| 13 |
if we subclass `AbstractUser` or define many-to-many relation with |
| 14 |
`auth.Group` (and give reverse relate name **groups**) we should be |
| 15 |
fine. |
| 16 |
|
| 17 |
By default, django-guardian monkey patches the user model to add some needed functionality. |
| 18 |
This can result in errors if guardian is imported into the models.py |
| 19 |
of the same app where the custom user model lives. |
| 20 |
|
| 21 |
To fix this, it is recommended to add the setting |
| 22 |
`GUARDIAN_MONKEY_PATCH_USER = False` in your settings.py and subclass |
| 23 |
`guardian.mixins.GuardianUserMixin` in your custom user model. |
| 24 |
|
| 25 |
!!! danger "important" |
| 26 |
|
| 27 |
`django-guardian` relies **heavily** on the `auth.User` model. |
| 28 |
Specifically it was build from the ground-up with relation between |
| 29 |
`auth.User` and `auth.Group` models. Retaining this relation is crucial |
| 30 |
for `guardian` - **without many to many User (custom or default) and |
| 31 |
auth.Group relation django-guardian will BREAK**. |
| 32 |
|
| 33 |
!!! tip "See Also" |
| 34 |
Read more about |
| 35 |
[customizing the Django User model](https://docs.djangoproject.com/en/stable/topics/auth/customizing/#substituting-a-custom-user-model) |
| 36 |
|
| 37 |
## Anonymous user creation |
| 38 |
|
| 39 |
It is also possible to override default behavior of how instance for |
| 40 |
anonymous user is created. In example, let's imagine we have our user model as follows: |
| 41 |
|
| 42 |
```python |
| 43 |
|
| 44 |
from django.contrib.auth.models import AbstractUser |
| 45 |
from django.db import models |
| 46 |
|
| 47 |
|
| 48 |
class CustomUser(AbstractUser): |
| 49 |
real_username = models.CharField(max_length=120, unique=True) |
| 50 |
birth_date = models.DateField() # field without default value |
| 51 |
|
| 52 |
USERNAME_FIELD = 'real_username' |
| 53 |
``` |
| 54 |
|
| 55 |
Note that there is a `birth_date` field defined at the model and it does |
| 56 |
not have a default value. It would fail to create anonymous user |
| 57 |
instance as default implementation cannot know anything about |
| 58 |
`CustomUser` model. |
| 59 |
|
| 60 |
In order to override the way anonymous instance is created we need to |
| 61 |
make `GUARDIAN_GET_INIT_ANONYMOUS_USER` pointing at our custom implementation. |
| 62 |
In example, let's define our init function: |
| 63 |
|
| 64 |
```python |
| 65 |
import datetime |
| 66 |
|
| 67 |
|
| 68 |
def get_anonymous_user_instance(User): |
| 69 |
return User(real_username='Anonymous', birth_date=datetime.date(1970, 1, 1)) |
| 70 |
``` |
| 71 |
|
| 72 |
and put it at `myapp/models.py`. Last step is to set proper configuration in our settings module: |
| 73 |
|
| 74 |
```python |
| 75 |
GUARDIAN_GET_INIT_ANONYMOUS_USER = 'myapp.models.get_anonymous_user_instance' |
| 76 |
``` |
| 77 |
|
| 78 |
If the field type of the our custom User class is not a CharField, we |
| 79 |
need to override the default value that the anonymous user is |
| 80 |
instantiated with. We can override this value by setting |
| 81 |
`ANONYMOUS_DEFAULT_USERNAME_VALUE` to |
| 82 |
a proper non-string default value, in our settings module: |
| 83 |
|
| 84 |
```python |
| 85 |
ANONYMOUS_DEFAULT_USERNAME_VALUE = non_string_object_value |
| 86 |
``` |