Initialize database routers for this context.
(self, routers: list[str | type] | None = None)
| 377 | _reset_timezone_cache() |
| 378 | |
| 379 | def _init_routers(self, routers: list[str | type] | None = None) -> None: |
| 380 | """Initialize database routers for this context.""" |
| 381 | from tortoise.router import router |
| 382 | |
| 383 | routers = routers or [] |
| 384 | router_cls = [] |
| 385 | for r in routers: |
| 386 | if isinstance(r, str): |
| 387 | try: |
| 388 | module_name, class_name = r.rsplit(".", 1) |
| 389 | router_cls.append(getattr(importlib.import_module(module_name), class_name)) |
| 390 | except Exception: |
| 391 | raise ConfigurationError(f"Can't import router from `{r}`") |
| 392 | elif isinstance(r, type): |
| 393 | router_cls.append(r) |
| 394 | else: |
| 395 | raise ConfigurationError("Router must be either str or type") |
| 396 | self._routers = router_cls |
| 397 | router.init_routers(router_cls) |
| 398 | |
| 399 | async def generate_schemas(self, safe: bool = True) -> None: |
| 400 | """ |
no test coverage detected