Architecture
FastAPI Mongo Admin follows a layered architecture inspired by Django’s admin, adapted for MongoDB document stores.
High-level overview
FastAPI App
│
├── mount_admin_app()
│ │
│ ├── create_admin_router() ← routes, Jinja2, HTMX
│ │ ├── AdminSite ← model registry
│ │ ├── ModelAdmin ← per-model config + hooks
│ │ └── CollectionRepository
│ │ ├── AsyncMotorBackend (mode="async")
│ │ └── SyncPyMongoBackend (mode="sync")
│ │
│ └── StaticFiles (/admin/static/)
│
└── Your application routes
Core components
- AdminSite
Central registry. Maps
collection_name→ModelAdmininstance. Supports custom views, template directories, and site branding.- ModelAdmin
Per-model configuration: list display, filters, fieldsets, permissions, hooks, and bulk actions. Subclass and override methods for customization.
- CollectionRepository
CRUD operations with field mapping translation, Pydantic validation,
list_select_relatedlookups, and changelist query building.- List filters
Sidebar filters on the changelist. Resolved from field names or custom
ListFiltersubclasses. Combined into a MongoDB query fragment.- Schema inference
Pydantic model fields are inspected to infer form widgets (text, number, checkbox, JSON editor, date, etc.) and validate submitted form data.
Request flow: changelist
User visits
GET /admin/{collection}/Router resolves
ModelAdminfromAdminSiteregistryCollectionRepository.list_documents()builds query from: -get_queryset()hook (base filter) - Search fields ($orregex on configured fields) - Active list filters (sidebar) - Date hierarchy params (year,month,day)Results are serialized (BSON → JSON-safe), related docs resolved
build_changelist_context()prepares Jinja2 contextTemplate renders (full page or HTMX partial for pagination)
Request flow: add/change form
GETrenders form with fieldsets fromget_fieldsets()POSTparses multipart form dataCSRF token verified (when session middleware is enabled)
parse_form_to_model()validates through Pydanticsave_model()hook mutates data before persistencetranslate_to_db()applies field mappingprepare_for_mongodb()converts Decimals, dates, etc.Backend inserts or updates the document
Redirect to changelist with flash cookies (type +
object_reprlabel)Changelist renders a one-time success banner and clears flash cookies
HTMX integration
The changelist uses HTMX for partial page updates. When an HTMX request is
detected (HX-Request header), only the result table partial is returned
instead of the full page. This enables fast pagination and filtering without
full page reloads.
Template resolution
Jinja2 searches template directories in this order:
Directories passed to
AdminSite(template_dirs=[...])Bundled package templates in
fastapi_mongo_admin/templates/
Per-model template overrides (change_list_template, etc.) reference paths
relative to these search directories.