Skip to content

Application

sqladmin.application.Admin

Bases: BaseAdminView

Main entrypoint to admin interface.

Usage
from fastapi import FastAPI
from sqladmin import Admin, ModelView

from mymodels import User # SQLAlchemy model


app = FastAPI()
admin = Admin(app, engine)


class UserAdmin(ModelView, model=User):
    column_list = [User.id, User.name]


admin.add_view(UserAdmin)

__init__(app, engine=None, session_maker=None, base_url='/admin', title='Admin', logo_url=None, favicon_url=None, middlewares=None, debug=False, templates_dir='templates', authentication_backend=None)

Parameters:

Name Type Description Default
app Starlette

Starlette or FastAPI application.

required
engine ENGINE_TYPE | None

SQLAlchemy engine instance.

None
session_maker sessionmaker | 'async_sessionmaker' | None

SQLAlchemy sessionmaker instance.

None
base_url str

Base URL for Admin interface.

'/admin'
title str

Admin title.

'Admin'
logo_url str | None

URL of logo to be displayed instead of title.

None
favicon_url str | None

URL of favicon to be displayed.

None

sqladmin.application.BaseAdmin

Base class for implementing Admin interface.

Danger

This class should almost never be used directly.

views: list[BaseView | ModelView] property

Get list of ModelView and BaseView instances lazily.

Returns:

Type Description
list[BaseView | ModelView]

List of ModelView and BaseView instances added to Admin.

add_view(view)

Add ModelView or BaseView classes to Admin. This is a shortcut that will handle both add_model_view and add_base_view.

add_model_view(view)

Add ModelView to the Admin.

Usage
from sqladmin import Admin, ModelView

class UserAdmin(ModelView, model=User):
    pass

admin.add_model_view(UserAdmin)

add_base_view(view)

Add BaseView to the Admin.

Usage
from sqladmin import BaseView, expose

class CustomAdmin(BaseView):
    name = "Custom Page"
    icon = "fa-solid fa-chart-line"

    @expose("/custom", methods=["GET"])
    async def test_page(self, request: Request):
        return await self.templates.TemplateResponse(request, "custom.html")

admin.add_base_view(CustomAdmin)

sqladmin.application.action(name, label=None, confirmation_message=None, *, include_in_schema=True, add_in_detail=True, add_in_list=True)

Decorate a ModelView function with this to:

  • expose it as a custom "action" route
  • add a button to the admin panel to invoke the action

When invoked from the admin panel, the following query parameter(s) are passed:

  • pks: the comma-separated list of selected object PKs - can be empty

Parameters:

Name Type Description Default
name str

Unique name for the action - should be alphanumeric, dash and underscore

required
label str | None

Human-readable text describing action

None
confirmation_message str | None

Message to show before confirming action

None
include_in_schema bool

Indicating if the endpoint be included in the schema

True
add_in_detail bool

Indicating if action should be dispalyed on model detail page

True
add_in_list bool

Indicating if action should be dispalyed on model list page

True