custom_user_models.rst 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. Custom user models
  2. ==================
  3. Custom user forms example
  4. ^^^^^^^^^^^^^^^^^^^^^^^^^
  5. This example shows how to add a text field and foreign key field to a custom user model
  6. and configure Wagtail user forms to allow the fields values to be updated.
  7. 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).
  8. .. code-block:: python
  9. from django.contrib.auth.models import AbstractUser
  10. class User(AbstractUser):
  11. country = models.CharField(verbose_name='country', max_length=255)
  12. status = models.ForeignKey(MembershipStatus, on_delete=models.SET_NULL, null=True, default=1)
  13. Add the app containing your user model to ``INSTALLED_APPS`` - it must be above the ``'wagtail.users'`` line,
  14. in order to override Wagtail's built-in templates - and set AUTH_USER_MODEL_ to reference
  15. your model. In this example the app is called ``users`` and the model is ``User``
  16. .. code-block:: python
  17. AUTH_USER_MODEL = 'users.User'
  18. Create your custom user 'create' and 'edit' forms in your app:
  19. .. code-block:: python
  20. from django import forms
  21. from django.utils.translation import gettext_lazy as _
  22. from wagtail.users.forms import UserEditForm, UserCreationForm
  23. from users.models import MembershipStatus
  24. class CustomUserEditForm(UserEditForm):
  25. country = forms.CharField(required=True, label=_("Country"))
  26. status = forms.ModelChoiceField(queryset=MembershipStatus.objects, required=True, label=_("Status"))
  27. class CustomUserCreationForm(UserCreationForm):
  28. country = forms.CharField(required=True, label=_("Country"))
  29. status = forms.ModelChoiceField(queryset=MembershipStatus.objects, required=True, label=_("Status"))
  30. Extend the Wagtail user 'create' and 'edit' templates. These extended templates should be placed in a
  31. template directory ``wagtailusers/users``.
  32. Template create.html:
  33. .. code-block:: html+django
  34. {% extends "wagtailusers/users/create.html" %}
  35. {% block extra_fields %}
  36. {% include "wagtailadmin/shared/field_as_li.html" with field=form.country %}
  37. {% include "wagtailadmin/shared/field_as_li.html" with field=form.status %}
  38. {% endblock extra_fields %}
  39. Template edit.html:
  40. .. code-block:: html+django
  41. {% extends "wagtailusers/users/edit.html" %}
  42. {% block extra_fields %}
  43. {% include "wagtailadmin/shared/field_as_li.html" with field=form.country %}
  44. {% include "wagtailadmin/shared/field_as_li.html" with field=form.status %}
  45. {% endblock extra_fields %}
  46. The ``extra_fields`` block allows fields to be inserted below the ``last_name`` field
  47. in the default templates. Other block overriding options exist to allow appending
  48. fields to the end or beginning of the existing fields, or to allow all the fields to
  49. be redefined.
  50. Add the wagtail settings to your project to reference the user form additions:
  51. .. code-block:: python
  52. WAGTAIL_USER_EDIT_FORM = 'users.forms.CustomUserEditForm'
  53. WAGTAIL_USER_CREATION_FORM = 'users.forms.CustomUserCreationForm'
  54. WAGTAIL_USER_CUSTOM_FIELDS = ['country', 'status']
  55. .. _AUTH_USER_MODEL: https://docs.djangoproject.com/en/stable/topics/auth/customizing/#substituting-a-custom-user-model