This class provides the administrative interface for managing user accounts, including specialized forms for creation and password modification. It defines comprehensive fieldsets for personal information, permissions, and important dates while implementing custom security logic for user addition and password management. The class also configures list views, filtering, and search capabilities tailored for user data.
Attributes
| Attribute | Type | Description |
|---|
| add_form_template | string = "admin/auth/user/add_form.html" | Path to the custom HTML template used for the user creation form view. |
| change_user_password_template | string = null | Path to the custom HTML template used for the password change view, defaulting to a standard auth template if null. |
| fieldsets | tuple | Definition of the layout and grouping of fields displayed in the user change form. |
| add_fieldsets | tuple | Definition of the layout and grouping of fields displayed specifically in the user creation form. |
| form | class = UserChangeForm | The form class used for editing existing user instances. |
| add_form | class = AdminUserCreationForm | The form class used for creating new user instances. |
| change_password_form | class = AdminPasswordChangeForm | The form class used to handle password updates for existing users. |
| list_display | tuple = ("username", "email", "first_name", "last_name", "is_staff") | List of fields to be displayed as columns in the user change list view. |
| list_filter | tuple = ("is_staff", "is_superuser", "is_active", "groups") | List of fields that can be used to filter the user list in the admin sidebar. |
| search_fields | tuple = ("username", "first_name", "last_name", "email") | List of fields to be searched when a query is entered in the admin search box. |
| ordering | tuple = ("username",) | The default field used to sort the user list in the admin interface. |
| filter_horizontal | tuple = ("groups", "user_permissions") | List of many-to-many fields that should use a horizontal filter interface for easier selection. |
Constructor
Signature
Methods
get_fieldsets()
@classmethod
def get_fieldsets(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest),
obj: [User](../models/user.md?sid=django_contrib_auth_models_user)
) - > tuple
Returns the fieldsets to use for the user admin form, switching between creation and editing modes.
Parameters
| Name | Type | Description |
|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The current request object |
| obj | [User](../models/user.md?sid=django_contrib_auth_models_user) | The user instance being edited, or None if creating a new user |
Returns
| Type | Description |
|---|
tuple | The fieldset configuration for the add form if obj is None, otherwise the standard change form fieldsets |
@classmethod
def get_form(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest),
obj: [User](../models/user.md?sid=django_contrib_auth_models_user),
kwargs: dict
) - > [ModelForm](../../../forms/models/modelform.md?sid=django_forms_models_modelform)
Use special form during user creation
Parameters
| Name | Type | Description |
|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The current request object |
| obj | [User](../models/user.md?sid=django_contrib_auth_models_user) | The user instance being edited, or None if creating a new user |
| kwargs | dict | Additional keyword arguments to pass to the form class |
Returns
| Type | Description |
|---|
[ModelForm](../../../forms/models/modelform.md?sid=django_forms_models_modelform) | The form class used to create or edit a user |
get_urls()
@classmethod
def get_urls() - > list
Extends the default admin URLs to include a custom password change endpoint for specific users.
Returns
| Type | Description |
|---|
list | A list of URL patterns including the custom user password change route |
lookup_allowed()
@classmethod
def lookup_allowed(
lookup: str,
value: str,
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > bool
Restricts filtering lookups to prevent sensitive password fields from being queried via URL parameters.
Parameters
| Name | Type | Description |
|---|
| lookup | str | The field lookup path being validated |
| value | str | The value associated with the lookup |
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The current request object |
Returns
| Type | Description |
|---|
bool | True if the lookup is permitted, False if it involves password fields |
add_view()
@classmethod
def add_view(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest),
form_url: str,
extra_context: dict
) - > [HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)
Handles the user creation view, ensuring the operation is wrapped in a database transaction for POST requests.
Parameters
| Name | Type | Description |
|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The current request object |
| form_url | str | The URL of the form being submitted |
| extra_context | dict | Additional template context variables |
Returns
| Type | Description |
|---|
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse) | The rendered user creation form or a redirect after successful creation |
user_change_password()
@classmethod
def user_change_password(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest),
id: str,
form_url: str
) - > [HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)
Provides a dedicated view for administrators to change or set a specific user's password.
Parameters
| Name | Type | Description |
|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The current request object |
| id | str | The primary key of the user whose password is being changed |
| form_url | str | The URL of the form being submitted |
Returns
| Type | Description |
|---|
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse) | A TemplateResponse for the password form or a redirect to the user's change page on success |
response_add()
@classmethod
def response_add(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest),
obj: [User](../models/user.md?sid=django_contrib_auth_models_user),
post_url_continue: str
) - > [HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)
Determine the HttpResponse for the add_view stage. It mostly defers to its superclass implementation but is customized because the User model has a slightly different workflow.
Parameters
| Name | Type | Description |
|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The current request object |
| obj | [User](../models/user.md?sid=django_contrib_auth_models_user) | The newly created user instance |
| post_url_continue | str | The URL to redirect to if the user chooses to continue editing |
Returns
| Type | Description |
|---|
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse) | The response object that redirects the user after a successful addition |