Customizing Django Admin Enhances User Management

In progress Django AppSec

Customizing Django Admin with UserAdmin improves user management by integrating additional fields and methods, thereby enhancing the functionality and usability of the Django Admin interface.

Introduction

Django's UserAdmin class provides a robust framework for managing user accounts and permissions. By subclassing UserAdmin, developers can extend its functionality to include custom fields, methods, and behaviors, thereby improving the overall user management experience.

Customizing UserAdmin

To customize the UserAdmin class, developers can override its attributes and methods to add or modify functionality. For example, adding custom fields to the user profile or integrating additional methods for managing user permissions.

Example Code

In the django-app/apps/accounts/admin.py file, the CustomUserAdmin class is updated to subclass django.contrib.auth.admin.UserAdmin. It keeps everything the built-in user admin provides and extends it: list_display shows built-in fields (first_name, last_name, is_staff) as columns in the user list, and fieldsets / add_fieldsets append an "Additional info" section for the custom age field on the edit and create pages.

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from . import models

@admin.register(models.CustomUser)
class CustomUserAdmin(UserAdmin):
    list_display = (
        "username",
        "email",
        "first_name",
        "last_name",
        "is_staff",
    )
    fieldsets = UserAdmin.fieldsets + (
        ("Additional info", {"fields": ("age",)}),
    )
    add_fieldsets = UserAdmin.add_fieldsets + (
        ("Additional info", {"fields": ("email", "age")}),
    )

Benefits of Customization

Customizing UserAdmin offers several benefits, including:

  • Enhanced User Management: By adding custom fields and methods, developers can provide a more comprehensive and user-friendly interface for managing user accounts.
  • Improved Security: Subclassing UserAdmin instead of a plain admin.ModelAdmin keeps Django's password handling: the create page asks for the password twice and hashes it, and the edit page links to a change-password form instead of exposing the password hash as an editable field.
  • Better Usability: Customizing UserAdmin can improve the usability of the Django Admin interface, making it easier for administrators to manage user accounts and permissions.

References

Review cards

Flashcard Why subclass `UserAdmin` rather than `admin.ModelAdmin` for a custom user model?

To keep Django's password handling: the create page asks for the password twice and hashes it, and the edit page links to a change-password form instead of showing the hash as an editable field.

Quiz Which `UserAdmin` attribute sets the fields shown on the *create user* page?
  • fieldsets
  • add_fieldsets
  • list_display
  • readonly_fields

fieldsets is the edit page and list_display the columns of the user list. Extending UserAdmin.add_fieldsets (rather than replacing it) adds a field like age without losing the defaults.