Custom User model¶
New in version 1.1.
Django 1.5 comes with the ability to customize default
- either by subclassing
AbstractUser or defining very own class. This can be
very powerful, it must be done with caution, though. Basically, if we subclass
AbstractUser or define many-to-many relation with
auth.Group (and give
reverse relate name groups) we should be fine.
By default django-guardian monkey patches the user model to add some needed functionality. This can result in errors if guardian is imported into the models.py of the same app where the custom user model lives.
To fix this, it is recommended to add the setting
GUARDIAN_MONKEY_PATCH = False
in your settings.py and add the
GuardianUserMixin to your custom user model.
django-guardian relies heavily on the
Specifically it was build from the ground-up with relation between
auth.Group models. Retaining this relation is crucial
guardian - without many to many User (custom or default) and
auth.Group relation django-guardian will BREAK.
Read more about customizing User model introduced in Django 1.5 here: https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#substituting-a-custom-user-model.
Anonymous user creation¶
It is also possible to override default behavior of how instance for anonymous user is created. In example, let’s imagine we have our user model as follows:
from django.contrib.auth.models import AbstractUser from django.db import models class CustomUser(AbstractUser): real_username = models.CharField(max_length=120, unique=True) birth_date = models.DateField() # field without default value USERNAME_FIELD = 'real_username'
Note that there is a
birth_date field defined at the model and it does not
have a default value. It would fail to create anonymous user instance as
default implementation cannot know anything about
In order to override the way anonymous instance is created we need to make
GUARDIAN_GET_INIT_ANONYMOUS_USER pointing at our custom
implementation. In example, let’s define our init function:
import datetime def get_anonymous_user_instance(User): return User(real_username='Anonymous', birth_date=datetime.date(1970, 1, 1))
and put it at
myapp/models.py. Last step is to set proper configuration in
our settings module:
GUARDIAN_GET_INIT_ANONYMOUS_USER = 'myapp.models.get_anonymous_user_instance'