What is AdminTab?
AdminTab is a base class that extends Django’s ModelAdmin to create custom tabs within a parent model’s admin interface. It allows you to organize complex admin interfaces by splitting functionality across multiple tabs, each displaying a different view or form for the same or related models.
AdminTab inherits from admin.ModelAdmin, so you can use all standard Django admin features like fields, inlines, fieldsets, and list_display.When to Use AdminTab
UseAdminTab when you need to:
- Display different forms or fieldsets for the same model in separate tabs
- Create multiple views of the same object with different configurations
- Organize complex admin forms into logical sections
- Provide alternative editing interfaces for the same model
Key Attributes
The display name for the tab in the admin interface. If not specified, the class name will be used.
Optionally specify a different model for this tab. If not specified, the parent model from
TabbedModelAdmin will be used.The template used to render the change form for this tab. The default template includes tab navigation.
The template used if this tab needs to display a list view.
Key Methods
get_tab_name()
Returns the display name for the tab. Falls back to the class name ifadmin_tab_name is not set.
get_tab_slug()
Returns a URL-safe slug version of the tab name, used for routing.get_model_perms(request)
Overridden to return an empty dictionary, which hides this admin class from the Django admin index page.Tabs are not meant to be accessed directly from the admin index. They only appear within the context of a
TabbedModelAdmin parent.How It Works
When you create anAdminTab subclass:
- It’s registered in a
TabbedModelAdmin’sadmin_tabslist - The tab is rendered as part of the parent model’s change view
- URL routing is handled automatically by
TabbedModelAdmin - The tab receives the parent object context and can display custom forms/fields
Code Example
Here’s a real example from the Django Admin Tabs source code:PollAdminStepis anAdminTabthat displays the Poll’s question field and inline choices- The
admin_tab_nameis set to “Poll” for display purposes - The tab includes Django’s standard inline functionality
- It’s registered with
PollAdminto appear as a tab in the Poll admin interface
Use Cases
Multiple Form Views
Create different tabs showing different fields of the same model:Custom Fieldsets
Organize complex models with different fieldset configurations per tab:Related Concepts
- AdminChangeListTab - For nested changelists of related models
- TabbedModelAdmin - The parent admin class that orchestrates tabs