2016-05-12 15:25:30 +02:00
Custom user models
==================
Custom user forms example
^^^^^^^^^^^^^^^^^^^^^^^^^
This example shows how to add a text field and foreign key field to a custom user model
and configure Wagtail user forms to allow the fields values to be updated.
2019-11-28 13:35:30 +01:00
Create a custom user model. This must at minimum inherit from `` AbstractBaseUser `` and `` PermissionsMixin `` . In this case we extend the `` AbstractUser `` class and add two fields. The foreign key references another model (not shown).
2016-05-12 15:25:30 +02:00
.. code-block :: python
2019-09-29 00:56:40 +02:00
from django.contrib.auth.models import AbstractUser
2020-10-02 19:08:50 +02:00
2016-05-12 15:25:30 +02:00
class User(AbstractUser):
country = models.CharField(verbose_name='country', max_length=255)
status = models.ForeignKey(MembershipStatus, on_delete=models.SET_NULL, null=True, default=1)
2020-05-18 11:39:50 +02:00
Add the app containing your user model to `` INSTALLED_APPS `` - it must be above the `` 'wagtail.users' `` line,
in order to override Wagtail's built-in templates - and set AUTH_USER_MODEL_ to reference
2016-05-12 15:25:30 +02:00
your model. In this example the app is called `` users `` and the model is `` User ``
.. code-block :: python
AUTH_USER_MODEL = 'users.User'
2019-02-14 16:14:44 +01:00
Create your custom user 'create' and 'edit' forms in your app:
2016-05-12 15:25:30 +02:00
.. code-block :: python
from django import forms
2020-03-24 17:52:40 +01:00
from django.utils.translation import gettext_lazy as _
2016-05-12 15:25:30 +02:00
2017-11-17 12:39:19 +01:00
from wagtail.users.forms import UserEditForm, UserCreationForm
2016-05-12 15:25:30 +02:00
from users.models import MembershipStatus
class CustomUserEditForm(UserEditForm):
country = forms.CharField(required=True, label=_("Country"))
status = forms.ModelChoiceField(queryset=MembershipStatus.objects, required=True, label=_("Status"))
class CustomUserCreationForm(UserCreationForm):
country = forms.CharField(required=True, label=_("Country"))
status = forms.ModelChoiceField(queryset=MembershipStatus.objects, required=True, label=_("Status"))
2019-02-14 16:14:44 +01:00
Extend the Wagtail user 'create' and 'edit' templates. These extended templates should be placed in a
2016-05-12 15:25:30 +02:00
template directory `` wagtailusers/users `` .
Template create.html:
.. code-block :: html+django
{% extends "wagtailusers/users/create.html" %}
{% block extra_fields %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.country %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.status %}
{% endblock extra_fields %}
Template edit.html:
.. code-block :: html+django
{% extends "wagtailusers/users/edit.html" %}
{% block extra_fields %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.country %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.status %}
{% endblock extra_fields %}
2019-02-14 01:37:54 +01:00
The `` extra_fields `` block allows fields to be inserted below the `` last_name `` field
2016-05-12 15:25:30 +02:00
in the default templates. Other block overriding options exist to allow appending
fields to the end or beginning of the existing fields, or to allow all the fields to
be redefined.
Add the wagtail settings to your project to reference the user form additions:
.. code-block :: python
WAGTAIL_USER_EDIT_FORM = 'users.forms.CustomUserEditForm'
WAGTAIL_USER_CREATION_FORM = 'users.forms.CustomUserCreationForm'
WAGTAIL_USER_CUSTOM_FIELDS = ['country', 'status']
2019-11-26 10:05:25 +01:00
.. _AUTH_USER_MODEL: https://docs.djangoproject.com/en/stable/topics/auth/customizing/#substituting-a-custom-user-model