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.
Tuple of custom URL patterns for the ModelAdmin. Format: ("path_to_view", "name_of_view", view_itself)
Fieldsets to use when creating a new object. If not set, falls back to fieldsets.
Field name to use for custom ordering functionality.
Whether to hide the ordering field from display.
list_horizontal_scrollbar_top
Show horizontal scrollbar at the top of the changelist table.
Add a submit button to list filters.
Display filters in a sheet (modal) interface.
Make the changelist table full width.
Disable the “select all” checkbox in the changelist.
Template path to render before the changelist.
Template path to render after the changelist.
change_form_before_template
Template path to render before the change form.
change_form_after_template
Template path to render after the change form.
change_form_outer_before_template
Template path to render before the outer change form container.
change_form_outer_after_template
Template path to render after the outer change form container.
Tuple of dataset classes to use in the change form.
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.
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.
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.
def get_formset_kwargs(
self,
request: HttpRequest,
obj: Model,
inline: InlineModelAdmin,
prefix: str
) -> dict[str, Any]
Returns kwargs for inline formsets, including pagination support.
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"),
}),
)