I want to create multiple admin panels for one fastapi app.
Adjust all the code to use dynamic "mount_name" instead of fixed names.
class CustomAdmin(Admin):
def __init__(
self,
app: Starlette,
engine: ENGINE_TYPE | None = None,
session_maker: sessionmaker | async_sessionmaker | None = None,
base_url: str = "/admin",
title: str = "Admin",
logo_url: str | None = None,
favicon_url: str | None = None,
middlewares: Sequence[Middleware] | None = None,
debug: bool = False,
templates_dir: str = "templates",
authentication_backend: AuthenticationBackend | None = None,
mount_name: str = "admin",
) -> None:
"""
Args:
app: Starlette or FastAPI application.
engine: SQLAlchemy engine instance.
session_maker: SQLAlchemy sessionmaker instance.
base_url: Base URL for Admin interface.
title: Admin title.
logo_url: URL of logo to be displayed instead of title.
favicon_url: URL of favicon to be displayed.
"""
super().__init__(
app=app,
engine=engine,
session_maker=session_maker,
base_url=base_url,
title=title,
logo_url=logo_url,
favicon_url=favicon_url,
templates_dir=templates_dir,
middlewares=middlewares,
authentication_backend=authentication_backend,
)
statics = StaticFiles(packages=["sqladmin"])
async def http_exception(
request: Request, exc: Exception
) -> Response | Awaitable[Response]:
assert isinstance(exc, HTTPException)
context = {
"status_code": exc.status_code,
"message": exc.detail,
}
return await self.templates.TemplateResponse(
request, "sqladmin/error.html", context, status_code=exc.status_code
)
routes = [
Mount("/statics", app=statics, name=f"{mount_name}_statics"),
Route("/", endpoint=self.index, name=f"{mount_name}_index"),
Route("/{identity}/list", endpoint=self.list, name=f"{mount_name}_list"),
Route(
"/{identity}/details/{pk:path}", endpoint=self.details, name=f"{mount_name}_details"
),
Route(
"/{identity}/delete",
endpoint=self.delete,
name=f"{mount_name}_delete",
methods=["DELETE"],
),
Route(
"/{identity}/create",
endpoint=self.create,
name=f"{mount_name}_create",
methods=["GET", "POST"],
),
Route(
"/{identity}/edit/{pk:path}",
endpoint=self.edit,
name=f"{mount_name}_edit",
methods=["GET", "POST"],
),
Route(
"/{identity}/export/{export_type}", endpoint=self.export, name=f"{mount_name}_export"
),
Route(
"/{identity}/ajax/lookup", endpoint=self.ajax_lookup, name=f"{mount_name}_ajax_lookup"
),
Route("/login", endpoint=self.login, name=f"{mount_name}_login", methods=["GET", "POST"]),
Route("/logout", endpoint=self.logout, name=f"{mount_name}_logout", methods=["GET"]),
]
self.admin.router.routes = routes
self.admin.exception_handlers = {HTTPException: http_exception}
self.admin.debug = debug
self.app.mount(base_url, app=self.admin, name=mount_name)
Checklist
Is your feature related to a problem? Please describe.
I want to create multiple admin panels for one fastapi app.
Describe the solution you would like.
Adjust all the code to use dynamic "mount_name" instead of fixed names.
Describe alternatives you considered
No response
Additional context