Skip to main content

UserAdmin

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

AttributeTypeDescription
add_form_templatestring = "admin/auth/user/add_form.html"Path to the custom HTML template used for the user creation form view.
change_user_password_templatestring = nullPath to the custom HTML template used for the password change view, defaulting to a standard auth template if null.
fieldsetstupleDefinition of the layout and grouping of fields displayed in the user change form.
add_fieldsetstupleDefinition of the layout and grouping of fields displayed specifically in the user creation form.
formclass = UserChangeFormThe form class used for editing existing user instances.
add_formclass = AdminUserCreationFormThe form class used for creating new user instances.
change_password_formclass = AdminPasswordChangeFormThe form class used to handle password updates for existing users.
list_displaytuple = ("username", "email", "first_name", "last_name", "is_staff")List of fields to be displayed as columns in the user change list view.
list_filtertuple = ("is_staff", "is_superuser", "is_active", "groups")List of fields that can be used to filter the user list in the admin sidebar.
search_fieldstuple = ("username", "first_name", "last_name", "email")List of fields to be searched when a query is entered in the admin search box.
orderingtuple = ("username",)The default field used to sort the user list in the admin interface.
filter_horizontaltuple = ("groups", "user_permissions")List of many-to-many fields that should use a horizontal filter interface for easier selection.

Constructor

Signature

def UserAdmin()

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

NameTypeDescription
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

TypeDescription
tupleThe fieldset configuration for the add form if obj is None, otherwise the standard change form fieldsets

get_form()

@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

NameTypeDescription
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
kwargsdictAdditional keyword arguments to pass to the form class

Returns

TypeDescription
[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

TypeDescription
listA 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

NameTypeDescription
lookupstrThe field lookup path being validated
valuestrThe value associated with the lookup
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The current request object

Returns

TypeDescription
boolTrue 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

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The current request object
form_urlstrThe URL of the form being submitted
extra_contextdictAdditional template context variables

Returns

TypeDescription
[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

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The current request object
idstrThe primary key of the user whose password is being changed
form_urlstrThe URL of the form being submitted

Returns

TypeDescription
[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

NameTypeDescription
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_continuestrThe URL to redirect to if the user chooses to continue editing

Returns

TypeDescription
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The response object that redirects the user after a successful addition