|
@@ -733,47 +733,45 @@ the "Model design considerations" note of :ref:`specifying-custom-user-model`.
|
|
|
Custom users and the built-in auth forms
|
|
|
----------------------------------------
|
|
|
|
|
|
-As you may expect, built-in Django's :ref:`forms <built-in-auth-forms>` and
|
|
|
-:ref:`views <built-in-auth-views>` make certain assumptions about the user
|
|
|
-model that they are working with.
|
|
|
+Django's built-in :ref:`forms <built-in-auth-forms>` and :ref:`views
|
|
|
+<built-in-auth-views>` make certain assumptions about the user model that they
|
|
|
+are working with.
|
|
|
|
|
|
-If your user model doesn't follow the same assumptions, it may be necessary to define
|
|
|
-a replacement form, and pass that form in as part of the configuration of the
|
|
|
-auth views.
|
|
|
-
|
|
|
-* :class:`~django.contrib.auth.forms.UserCreationForm`
|
|
|
-
|
|
|
- Depends on the :class:`~django.contrib.auth.models.User` model.
|
|
|
- Must be re-written for any custom user model.
|
|
|
-
|
|
|
-* :class:`~django.contrib.auth.forms.UserChangeForm`
|
|
|
-
|
|
|
- Depends on the :class:`~django.contrib.auth.models.User` model.
|
|
|
- Must be re-written for any custom user model.
|
|
|
-
|
|
|
-* :class:`~django.contrib.auth.forms.AuthenticationForm`
|
|
|
-
|
|
|
- Works with any subclass of :class:`~django.contrib.auth.models.AbstractBaseUser`,
|
|
|
- and will adapt to use the field defined in ``USERNAME_FIELD``.
|
|
|
+The following forms are compatible with any subclass of
|
|
|
+:class:`~django.contrib.auth.models.AbstractBaseUser`:
|
|
|
|
|
|
-* :class:`~django.contrib.auth.forms.PasswordResetForm`
|
|
|
+* :class:`~django.contrib.auth.forms.AuthenticationForm`: Uses the username
|
|
|
+ field specified by :attr:`~models.CustomUser.USERNAME_FIELD`.
|
|
|
+* :class:`~django.contrib.auth.forms.SetPasswordForm`
|
|
|
+* :class:`~django.contrib.auth.forms.PasswordChangeForm`
|
|
|
+* :class:`~django.contrib.auth.forms.AdminPasswordChangeForm`
|
|
|
|
|
|
- Assumes that the user model has a field named ``email`` that can be used to
|
|
|
- identify the user and a boolean field named ``is_active`` to prevent
|
|
|
- password resets for inactive users.
|
|
|
+The following forms make assumptions about the user model and can be used as-is
|
|
|
+if those assumptions are met:
|
|
|
|
|
|
-* :class:`~django.contrib.auth.forms.SetPasswordForm`
|
|
|
+* :class:`~django.contrib.auth.forms.PasswordResetForm`: Assumes that the user
|
|
|
+ model has a field named ``email`` that can be used to identify the user and a
|
|
|
+ boolean field named ``is_active`` to prevent password resets for inactive
|
|
|
+ users.
|
|
|
|
|
|
- Works with any subclass of :class:`~django.contrib.auth.models.AbstractBaseUser`
|
|
|
+Finally, the following forms are tied to
|
|
|
+:class:`~django.contrib.auth.models.User` and need to be rewritten or extended
|
|
|
+to work with a custom user model:
|
|
|
|
|
|
-* :class:`~django.contrib.auth.forms.PasswordChangeForm`
|
|
|
+* :class:`~django.contrib.auth.forms.UserCreationForm`
|
|
|
+* :class:`~django.contrib.auth.forms.UserChangeForm`
|
|
|
|
|
|
- Works with any subclass of :class:`~django.contrib.auth.models.AbstractBaseUser`
|
|
|
+If your custom user model is a simple subclass of ``AbstractUser``, then you
|
|
|
+can extend these forms in this manner::
|
|
|
|
|
|
-* :class:`~django.contrib.auth.forms.AdminPasswordChangeForm`
|
|
|
+ from django.contrib.auth.forms import UserCreationForm
|
|
|
+ from myapp.models import CustomUser
|
|
|
|
|
|
- Works with any subclass of :class:`~django.contrib.auth.models.AbstractBaseUser`
|
|
|
+ class CustomUserCreationForm(UserCreationForm):
|
|
|
|
|
|
+ class Meta(UserCreationForm.Meta):
|
|
|
+ model = CustomUser
|
|
|
+ fields = UserCreationForm.Meta.fields + ('custom_field',)
|
|
|
|
|
|
Custom users and :mod:`django.contrib.admin`
|
|
|
--------------------------------------------
|