Skip to content

Routing

URL structure

For each registered model (e.g. Product), the following routes are generated under your mount prefix (e.g. /admin):

Method Pattern View Controlled by
GET /admin/product/ List view can_list
GET /admin/product/create Create form can_create
POST /admin/product/ Handle create can_create
GET /admin/product/{id} Detail view can_detail
GET /admin/product/{id}/edit Edit form can_edit
PUT /admin/product/{id} Handle update can_edit
DELETE /admin/product/{id} Delete action can_delete

Routes are only registered for operations enabled by AdminOptions.

HyperAdminRouter

hyperadmin.routing.HyperAdminRouter

Generates and owns all FastAPI routers for HyperAdmin.

Called internally by Admin.mount(). Iterates SiteRegistry and calls create_admin_router for each registered model.

Parameters:

Name Type Description Default
engine Any

The async SQLAlchemy engine passed to every adapter.

required
templates Jinja2Templates

The shared Jinja2Templates instance used across all views.

required
Source code in src/hyperadmin/routing.py
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
class HyperAdminRouter:
    """Generates and owns all FastAPI routers for HyperAdmin.

    Called internally by ``Admin.mount()``. Iterates ``SiteRegistry`` and
    calls ``create_admin_router`` for each registered model.

    Args:
        engine: The async SQLAlchemy engine passed to every adapter.
        templates: The shared ``Jinja2Templates`` instance used across all views.
    """

    def __init__(
        self,
        engine: Any,
        templates: Jinja2Templates,
        permission_checker: Any = None,
        storage: Any = None,
    ):
        self.engine = engine
        self.permission_checker = permission_checker
        self.storage = storage
        # Enable global whitespace trimming
        templates.env.trim_blocks = True
        templates.env.lstrip_blocks = True
        self.templates = templates
        self.routers: list[APIRouter] = []

    def generate_routes(self) -> None:
        """Generates the routes for the registered models."""
        from hyperadmin.core.registry import site

        self.routers = []
        nav_items: list[dict[str, str]] = []

        # Add the main admin dashboard route
        dashboard_router = APIRouter()
        dashboard_router.add_api_route(
            "/",
            self.get_admin_dashboard_view(),
            methods=["GET"],
            name="admin-dashboard",
        )
        self.routers.append(dashboard_router)

        for model, admin_class in site._registry.items():
            admin_instance = admin_class(model)
            # Prioritize options set on admin_class, then fall back to defaults
            options = getattr(admin_class, "options", None) or AdminOptions()
            # If admin_class has list_filter set directly (legacy or class-style)
            if hasattr(admin_class, "list_filter") and options.list_filter is None:
                class_filter = getattr(admin_class, "list_filter", None)
                if class_filter:
                    options.list_filter = class_filter

            form_include = _extract_column_names(getattr(admin_class, "form_columns", None), model)
            form_create_exclude = _extract_column_names(
                getattr(admin_class, "form_create_exclude", None), model
            )
            column_list = _extract_column_names(
                getattr(admin_class, "column_list", None) or getattr(admin_class, "list", None),
                model,
            )

            resolved_column_list, resolved_search_fields, options, field_labels = (
                _resolve_smart_defaults(model, options, column_list)
            )

            actions = collect_actions(admin_class)

            router = create_admin_router(
                model=model,
                admin_class=admin_class,
                admin_instance=admin_instance,
                options=options,
                engine=self.engine,
                templates=self.templates,
                form_include=form_include,
                form_create_exclude=form_create_exclude,
                column_list=resolved_column_list,
                permission_checker=self.permission_checker,
                actions=actions,
                search_fields=resolved_search_fields,
                field_labels=field_labels,
                storage=self.storage,
            )
            self.routers.append(router)

            model_name = model.__name__
            # Resolve nav label: prefer verbose_name_plural / verbose_name
            # (which may be LazyProxy instances) over the legacy name_plural /
            # name attributes.  Do NOT call str() here — lazy strings must
            # remain lazy so they render in the request locale at template time.
            legacy_name_plural = getattr(admin_class, "name_plural", None)
            if legacy_name_plural:
                nav_name = legacy_name_plural
            elif hasattr(admin_class, "get_verbose_name_plural"):
                nav_name = admin_class.get_verbose_name_plural(model)
            else:
                nav_name = getattr(admin_class, "name", model_name) + "s"
            nav_items.append(
                {
                    "name": nav_name,
                    "url": f"/{model_name.lower()}",
                    "icon": getattr(admin_class, "icon", ""),
                }
            )

        self.templates.env.globals["nav_items"] = nav_items

    def get_admin_dashboard_view(self):
        from hyperadmin.views.dynamic import admin_dashboard

        async def admin_dashboard_view(request: Request):
            return await admin_dashboard(request, self.templates)

        return admin_dashboard_view

    def get_routers(self) -> list[APIRouter]:
        """Returns the list of generated APIRouters."""
        return self.routers

generate_routes()

Generates the routes for the registered models.

Source code in src/hyperadmin/routing.py
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
def generate_routes(self) -> None:
    """Generates the routes for the registered models."""
    from hyperadmin.core.registry import site

    self.routers = []
    nav_items: list[dict[str, str]] = []

    # Add the main admin dashboard route
    dashboard_router = APIRouter()
    dashboard_router.add_api_route(
        "/",
        self.get_admin_dashboard_view(),
        methods=["GET"],
        name="admin-dashboard",
    )
    self.routers.append(dashboard_router)

    for model, admin_class in site._registry.items():
        admin_instance = admin_class(model)
        # Prioritize options set on admin_class, then fall back to defaults
        options = getattr(admin_class, "options", None) or AdminOptions()
        # If admin_class has list_filter set directly (legacy or class-style)
        if hasattr(admin_class, "list_filter") and options.list_filter is None:
            class_filter = getattr(admin_class, "list_filter", None)
            if class_filter:
                options.list_filter = class_filter

        form_include = _extract_column_names(getattr(admin_class, "form_columns", None), model)
        form_create_exclude = _extract_column_names(
            getattr(admin_class, "form_create_exclude", None), model
        )
        column_list = _extract_column_names(
            getattr(admin_class, "column_list", None) or getattr(admin_class, "list", None),
            model,
        )

        resolved_column_list, resolved_search_fields, options, field_labels = (
            _resolve_smart_defaults(model, options, column_list)
        )

        actions = collect_actions(admin_class)

        router = create_admin_router(
            model=model,
            admin_class=admin_class,
            admin_instance=admin_instance,
            options=options,
            engine=self.engine,
            templates=self.templates,
            form_include=form_include,
            form_create_exclude=form_create_exclude,
            column_list=resolved_column_list,
            permission_checker=self.permission_checker,
            actions=actions,
            search_fields=resolved_search_fields,
            field_labels=field_labels,
            storage=self.storage,
        )
        self.routers.append(router)

        model_name = model.__name__
        # Resolve nav label: prefer verbose_name_plural / verbose_name
        # (which may be LazyProxy instances) over the legacy name_plural /
        # name attributes.  Do NOT call str() here — lazy strings must
        # remain lazy so they render in the request locale at template time.
        legacy_name_plural = getattr(admin_class, "name_plural", None)
        if legacy_name_plural:
            nav_name = legacy_name_plural
        elif hasattr(admin_class, "get_verbose_name_plural"):
            nav_name = admin_class.get_verbose_name_plural(model)
        else:
            nav_name = getattr(admin_class, "name", model_name) + "s"
        nav_items.append(
            {
                "name": nav_name,
                "url": f"/{model_name.lower()}",
                "icon": getattr(admin_class, "icon", ""),
            }
        )

    self.templates.env.globals["nav_items"] = nav_items

get_routers()

Returns the list of generated APIRouters.

Source code in src/hyperadmin/routing.py
366
367
368
def get_routers(self) -> list[APIRouter]:
    """Returns the list of generated APIRouters."""
    return self.routers