Sets up Tortoise-ORM: loads apps and models, configures database connections but does not connect to the database yet. The actual connection or connection pool is established lazily on first query execution. You can configure using only one of ``config``, ``config_f
(
cls,
config: dict[str, Any] | TortoiseConfig | None = None,
config_file: str | None = None,
_create_db: bool = False,
db_url: str | None = None,
modules: dict[str, Iterable[str | ModuleType]] | None = None,
use_tz: bool = True,
timezone: str = "UTC",
routers: list[str | type] | None = None,
table_name_generator: Callable[[type[Model]], str] | None = None,
init_connections: bool = True,
_enable_global_fallback: bool = False,
)
| 295 | |
| 296 | @classmethod |
| 297 | async def init( |
| 298 | cls, |
| 299 | config: dict[str, Any] | TortoiseConfig | None = None, |
| 300 | config_file: str | None = None, |
| 301 | _create_db: bool = False, |
| 302 | db_url: str | None = None, |
| 303 | modules: dict[str, Iterable[str | ModuleType]] | None = None, |
| 304 | use_tz: bool = True, |
| 305 | timezone: str = "UTC", |
| 306 | routers: list[str | type] | None = None, |
| 307 | table_name_generator: Callable[[type[Model]], str] | None = None, |
| 308 | init_connections: bool = True, |
| 309 | _enable_global_fallback: bool = False, |
| 310 | ) -> TortoiseContext: |
| 311 | """ |
| 312 | Sets up Tortoise-ORM: loads apps and models, configures database connections but does not |
| 313 | connect to the database yet. The actual connection or connection pool is established |
| 314 | lazily on first query execution. |
| 315 | |
| 316 | You can configure using only one of ``config``, ``config_file`` |
| 317 | and ``(db_url, modules)``. |
| 318 | |
| 319 | :param config: |
| 320 | Dict containing config or ``TortoiseConfig``: |
| 321 | |
| 322 | .. admonition:: Example |
| 323 | |
| 324 | .. code-block:: python3 |
| 325 | |
| 326 | { |
| 327 | 'connections': { |
| 328 | # Dict format for connection |
| 329 | 'default': { |
| 330 | 'engine': 'tortoise.backends.asyncpg', |
| 331 | 'credentials': { |
| 332 | 'host': 'localhost', |
| 333 | 'port': '5432', |
| 334 | 'user': 'tortoise', |
| 335 | 'password': 'qwerty123', |
| 336 | 'database': 'test', |
| 337 | } |
| 338 | }, |
| 339 | # Using a DB_URL string |
| 340 | 'default': 'postgres://postgres:qwerty123@localhost:5432/test' |
| 341 | }, |
| 342 | 'apps': { |
| 343 | 'my_app': { |
| 344 | 'models': ['__main__'], |
| 345 | # If no default_connection specified, defaults to 'default' |
| 346 | 'default_connection': 'default', |
| 347 | } |
| 348 | }, |
| 349 | 'routers': ['path.router1', 'path.router2'], |
| 350 | 'use_tz': False, |
| 351 | 'timezone': 'UTC' |
| 352 | } |
| 353 | |
| 354 | :param config_file: |