2
0

custom_user_models.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. In this case we extend the ``AbstractUser`` class and add
  8. two fields. The foreign key references another model (not shown).
  9. .. code-block:: python
  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`` and set AUTH_USER_MODEL_ to reference
  14. your model. In this example the app is called ``users`` and the model is ``User``
  15. .. code-block:: python
  16. AUTH_USER_MODEL = 'users.User'
  17. Create your custom user create and edit forms in your app:
  18. .. code-block:: python
  19. from django import forms
  20. from django.utils.translation import ugettext_lazy as _
  21. from wagtail.wagtailusers.forms import UserEditForm, UserCreationForm
  22. from users.models import MembershipStatus
  23. class CustomUserEditForm(UserEditForm):
  24. country = forms.CharField(required=True, label=_("Country"))
  25. status = forms.ModelChoiceField(queryset=MembershipStatus.objects, required=True, label=_("Status"))
  26. class CustomUserCreationForm(UserCreationForm):
  27. country = forms.CharField(required=True, label=_("Country"))
  28. status = forms.ModelChoiceField(queryset=MembershipStatus.objects, required=True, label=_("Status"))
  29. Extend the Wagtail user create and edit templates. These extended template should be placed in a
  30. template directory ``wagtailusers/users``.
  31. Template create.html:
  32. .. code-block:: html+django
  33. {% extends "wagtailusers/users/create.html" %}
  34. {% block extra_fields %}
  35. {% include "wagtailadmin/shared/field_as_li.html" with field=form.country %}
  36. {% include "wagtailadmin/shared/field_as_li.html" with field=form.status %}
  37. {% endblock extra_fields %}
  38. .. note::
  39. Using ``{% extends %}`` in this way on a template you're currently overriding is only supported in Django 1.9 and above. On Django 1.8, you will need to use `django-overextends <https://github.com/stephenmcd/django-overextends>`_ instead.
  40. Template edit.html:
  41. .. code-block:: html+django
  42. {% extends "wagtailusers/users/edit.html" %}
  43. {% block extra_fields %}
  44. {% include "wagtailadmin/shared/field_as_li.html" with field=form.country %}
  45. {% include "wagtailadmin/shared/field_as_li.html" with field=form.status %}
  46. {% endblock extra_fields %}
  47. The ``extra_fields`` block allows fields to be inserted below the last name field
  48. in the default templates. Other block overriding options exist to allow appending
  49. fields to the end or beginning of the existing fields, or to allow all the fields to
  50. be redefined.
  51. Add the wagtail settings to your project to reference the user form additions:
  52. .. code-block:: python
  53. WAGTAIL_USER_EDIT_FORM = 'users.forms.CustomUserEditForm'
  54. WAGTAIL_USER_CREATION_FORM = 'users.forms.CustomUserCreationForm'
  55. WAGTAIL_USER_CUSTOM_FIELDS = ['country', 'status']
  56. .. _AUTH_USER_MODEL: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model