Skip to main content

Overview

The ModelAdmin class extends Django’s base ModelAdmin with Unfold-specific features for enhanced admin interface customization.

Class Signature

from unfold.admin import ModelAdmin

class ModelAdmin(
    BaseModelAdminMixin,
    ActionModelAdminMixin,
    DatasetModelAdminMixin,
    BaseModelAdmin
)
Located in unfold/admin.py:51

Attributes

action_form
Type[Form]
default:"ActionForm"
Custom form class for actions in the changelist.
custom_urls
tuple
default:"()"
Tuple of custom URL patterns for the ModelAdmin. Format: ("path_to_view", "name_of_view", view_itself)
add_fieldsets
tuple
default:"()"
Fieldsets to use when creating a new object. If not set, falls back to fieldsets.
ordering_field
str | None
default:"None"
Field name to use for custom ordering functionality.
hide_ordering_field
bool
default:"False"
Whether to hide the ordering field from display.
list_horizontal_scrollbar_top
bool
default:"False"
Show horizontal scrollbar at the top of the changelist table.
list_filter_submit
bool
default:"False"
Add a submit button to list filters.
list_filter_sheet
bool
default:"True"
Display filters in a sheet (modal) interface.
list_fullwidth
bool
default:"False"
Make the changelist table full width.
list_disable_select_all
bool
default:"False"
Disable the “select all” checkbox in the changelist.
list_before_template
str | None
default:"None"
Template path to render before the changelist.
list_after_template
str | None
default:"None"
Template path to render after the changelist.
change_form_before_template
str | None
default:"None"
Template path to render before the change form.
change_form_after_template
str | None
default:"None"
Template path to render after the change form.
change_form_outer_before_template
str | None
default:"None"
Template path to render before the outer change form container.
change_form_outer_after_template
str | None
default:"None"
Template path to render after the outer change form container.
change_form_datasets
tuple
default:"()"
Tuple of dataset classes to use in the change form.
compressed_fields
bool
default:"False"
Use compressed layout for form fields.
Show the “Add” link in related field widgets.
readonly_preprocess_fields
dict[str, Callable | str]
default:"{}"
Dictionary mapping field names to preprocessing functions for readonly fields.
warn_unsaved_form
bool
default:"False"
Show a warning when leaving a page with unsaved form changes.
checks_class
Type
default:"UnfoldModelAdminChecks"
Custom checks class for admin validation.

Methods

changelist_view

def changelist_view(
    self,
    request: HttpRequest,
    extra_context: dict[str, str] | None = None
) -> TemplateResponse
Renders the changelist view with Unfold customizations.
request
HttpRequest
required
The HTTP request object.
extra_context
dict[str, str] | None
Additional context data for the template.

get_list_display

def get_list_display(self, request: HttpRequest) -> list | tuple
Returns the list of fields to display in the changelist.

get_fieldsets

def get_fieldsets(
    self,
    request: HttpRequest,
    obj: Model | None = None
) -> FieldsetsType
Returns fieldsets for the change form. Uses add_fieldsets for new objects if defined.

get_custom_urls

def get_custom_urls(self) -> tuple[tuple[str, str, View], ...]
Returns custom URLs defined for this ModelAdmin.

get_urls

def get_urls(self) -> list[URLPattern]
Returns all URL patterns including custom URLs and action URLs.

get_action_choices

def get_action_choices(
    self,
    request: HttpRequest,
    default_choices: list[tuple[str, str]] = BLANK_CHOICE_DASH
) -> list[tuple[str, str]]
Returns available action choices for the changelist.

response_change

def response_change(
    self,
    request: HttpRequest,
    obj: Model
) -> HttpResponse
Handles the response after changing an object. Supports ?next= parameter for custom redirects.

response_add

def response_add(
    self,
    request: HttpRequest,
    obj: Model,
    post_url_continue: str | None = None
) -> HttpResponse
Handles the response after adding an object. Supports ?next= parameter for custom redirects.

get_changelist

def get_changelist(self, request: HttpRequest, **kwargs: Any) -> ChangeList
Returns the ChangeList class to use for this ModelAdmin.

get_formset_kwargs

def get_formset_kwargs(
    self,
    request: HttpRequest,
    obj: Model,
    inline: InlineModelAdmin,
    prefix: str
) -> dict[str, Any]
Returns kwargs for inline formsets, including pagination support.

get_changeform_datasets

def get_changeform_datasets(
    self,
    request: HttpRequest
) -> list[type[BaseDataset]]
Returns the list of dataset classes for the change form.

Usage Example

from django.contrib import admin
from unfold.admin import ModelAdmin
from .models import Article

@admin.register(Article)
class ArticleAdmin(ModelAdmin):
    list_display = ["title", "author", "created_at"]
    list_filter = ["status", "created_at"]
    list_filter_submit = True
    list_fullwidth = True
    warn_unsaved_form = True
    
    add_fieldsets = (
        (None, {
            "fields": ("title", "content"),
        }),
    )
    
    fieldsets = (
        (None, {
            "fields": ("title", "slug", "content"),
        }),
        ("Metadata", {
            "fields": ("author", "status", "created_at"),
        }),
    )

Build docs developers (and LLMs) love