SetUnusablePasswordMixin
Form mixin that allows setting an unusable password for a user.
Attributes
| Attribute | Type | Description |
|---|---|---|
| usable_password_help_text | string | Whether the user will be able to authenticate using a password or not. If disabled, they may still be able to authenticate using other backends, such as Single Sign-On or LDAP. |
Methods
create_usable_password_field()
@classmethod
def create_usable_password_field(
help_text: string = usable_password_help_text
) - > forms.ChoiceField
Creates a form field that allows users to toggle whether password-based authentication is enabled or disabled.
Parameters
| Name | Type | Description |
|---|---|---|
| help_text | string = usable_password_help_text | The explanatory text displayed alongside the field to describe the implications of disabling password authentication. |
Returns
| Type | Description |
|---|---|
forms.ChoiceField | A radio select choice field configured with 'Enabled' and 'Disabled' options. |
validate_passwords()
@classmethod
def validate_passwords(
password1_field_name: string = "password1",
password2_field_name: string = "password2",
usable_password_field_name: string = "usable_password"
) - > null
Validates the password fields based on whether the usable password toggle is enabled. If disabled, it bypasses standard password validation; otherwise, it ensures required password fields are present and valid.
Parameters
| Name | Type | Description |
|---|---|---|
| password1_field_name | string = "password1" | The name of the first password input field in the form. |
| password2_field_name | string = "password2" | The name of the second password input field used for confirmation. |
| usable_password_field_name | string = "usable_password" | The name of the field indicating if the user should have a usable password. |
Returns
| Type | Description |
|---|---|
null | This method does not return a value but updates the form's cleaned_data and error collection. |
validate_password_for_user()
@classmethod
def validate_password_for_user(
user: [User](../models/user.md?sid=django_contrib_auth_models_user),
**kwargs: dict
) - > null
Performs user-specific password validation only if the form is configured to set a usable password.
Parameters
| Name | Type | Description |
|---|---|---|
| user | [User](../models/user.md?sid=django_contrib_auth_models_user) | The user instance against which the password complexity or policy is being validated. |
| **kwargs | dict | Additional keyword arguments passed to the superclass validation method. |
Returns
| Type | Description |
|---|---|
null | This method does not return a value but may raise ValidationErrors via the superclass. |
set_password_and_save()
@classmethod
def set_password_and_save(
user: [User](../models/user.md?sid=django_contrib_auth_models_user),
commit: boolean = True,
**kwargs: dict
) - > [User](../models/user.md?sid=django_contrib_auth_models_user)
Updates the user's password or sets it as unusable based on the form selection, then optionally persists the user to the database.
Parameters
| Name | Type | Description |
|---|---|---|
| user | [User](../models/user.md?sid=django_contrib_auth_models_user) | The user instance whose password credentials are being updated. |
| commit | boolean = True | Determines whether the user instance should be immediately saved to the database. |
| **kwargs | dict | Additional keyword arguments passed to the superclass password setting method. |
Returns
| Type | Description |
|---|---|
[User](../models/user.md?sid=django_contrib_auth_models_user) | The updated user instance with the new password state applied. |