ModelAdmin
ModelAdmin is the configuration class for each registered model. It controls
changelist columns, filters, forms, permissions, and lifecycle hooks.
Basic configuration
class ProductAdmin(ModelAdmin):
model = Product # Pydantic model
collection_name = "products" # MongoDB collection (required)
list_display = ["name", "price", "active"]
list_display_links = ["name"]
list_filter = ["category", "active"]
search_fields = ["name", "category"]
list_per_page = 25
ordering = ["-created_at"]
Configuration reference
Option |
Description |
|---|---|
|
Pydantic |
|
MongoDB collection name (required) |
|
Changelist columns — field names or |
|
Clickable columns (default: first column) |
|
Fields editable inline on the changelist |
|
Field names or |
|
Fields searched with case-insensitive regex |
|
Pagination size (default: 25) |
|
Max rows when “show all” is selected (default: 200) |
|
Default sort; prefix with |
|
Field name for year/month/day drill-down navigation |
|
|
|
Grouped change form layout |
|
Non-editable fields on change forms |
|
Model field → MongoDB key mapping |
|
Enabled bulk action method names |
|
Choice lookups for filters and select widgets |
|
Per-field widget and HTML attribute overrides |
|
Display format for |
|
Display format for |
Template overrides
Attribute |
Default template |
|---|---|
|
|
|
|
|
|
Display columns
Field names
Reference model fields directly in list_display:
list_display = ["name", "price", "active"]
@display decorator
Add computed columns with custom rendering:
from fastapi_mongo_admin import display
@display(description="Price", ordering="price")
def price_display(self, obj: dict) -> str:
return f"${obj.get('price', 0):,.2f}"
The ordering parameter enables sortable columns (maps to the underlying field).
Fieldsets
Group fields on add/change forms:
fieldsets = [
("Basic info", {"fields": ["name", "sku", "description"]}),
("Pricing", {"fields": ["price", "compare_at_price", "is_taxable"]}),
("Timestamps", {"fields": ["created_at", "updated_at"]}),
]
If fieldsets is not set, all model fields appear in a single group.
Readonly fields
readonly_fields = ["created_at", "updated_at", "order_number"]
Readonly fields are displayed but not submitted on update. Existing values are preserved when the field is absent from the form POST.
Choices
Provide discrete values for filters and select form widgets:
choices = {
"status": [
("draft", "Draft"),
("published", "Published"),
("archived", "Archived"),
],
}
Enum fields can be generated dynamically:
choices = {
"status": [(s.value, s.name.replace("_", " ").title()) for s in ProductStatus],
}
Date hierarchy
Enable year → month → day drill-down on a date/datetime field:
date_hierarchy = "published_at"
Navigation links appear above the changelist. Query params year, month,
and day filter results progressively.
Date and time display
date and datetime fields are auto-formatted on changelists and readonly
form fields. See Date, Time, and Save Messages for defaults and customization.
Object representation
object_repr(request, obj) returns a human-readable label for an object. It is
used for:
Save success messages (
"Widget Pro" was saved successfully.)Bulk delete confirmation rows
Override to customize the label shown in flash messages.
Configurable methods
Override these methods for dynamic behavior:
Method |
Purpose |
|---|---|
|
Dynamic changelist columns |
|
Dynamic link columns |
|
Dynamic search fields |
|
Dynamic default ordering |
|
Context-dependent readonly fields |
|
Dynamic form layout |
|
Custom filter instantiation |
|
Control which bulk actions are available |
|
Add base MongoDB filter to all queries |
|
Dynamic widget overrides |
|
Per-field customization hook |
|
Custom date display formatting |
|
Custom datetime display formatting |
|
Human-readable label for flash messages |
|
Resolve changelist cell values (includes date/datetime formatting) |
See Lifecycle Hooks for lifecycle hooks (save_model, delete_model, etc.).
See Date, Time, and Save Messages for date/time display and save notifications.
See API Reference for the full ModelAdmin API.