MCPcopy
hub / github.com/tortoise/tortoise-orm / init

Method init

tortoise/context.py:259–368  ·  view source on GitHub ↗

Initialize this context with database configuration. You can configure using one of: ``config``, ``config_file``, or ``(db_url, modules)``. This method is self-sufficient and can be used directly in tests without going through Tortoise.init(): async wi

(
        self,
        config: dict[str, Any] | TortoiseConfig | None = None,
        *,
        config_file: str | None = None,
        db_url: str | None = None,
        modules: dict[str, Iterable[str | ModuleType]] | None = None,
        _create_db: bool = False,
        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,
    )

Source from the content-addressed store, hash-verified

257 return config
258
259 async def init(
260 self,
261 config: dict[str, Any] | TortoiseConfig | None = None,
262 *,
263 config_file: str | None = None,
264 db_url: str | None = None,
265 modules: dict[str, Iterable[str | ModuleType]] | None = None,
266 _create_db: bool = False,
267 use_tz: bool = True,
268 timezone: str = "UTC",
269 routers: list[str | type] | None = None,
270 table_name_generator: Callable[[type[Model]], str] | None = None,
271 init_connections: bool = True,
272 _enable_global_fallback: bool = False,
273 ) -> None:
274 """
275 Initialize this context with database configuration.
276
277 You can configure using one of: ``config``, ``config_file``, or ``(db_url, modules)``.
278
279 This method is self-sufficient and can be used directly in tests without
280 going through Tortoise.init():
281
282 async with TortoiseContext() as ctx:
283 await ctx.init(db_url="sqlite://:memory:", modules={"models": ["myapp.models"]})
284 # Run tests...
285
286 Args:
287 config: Full configuration dict or TortoiseConfig with 'connections' and 'apps' keys.
288 config_file: Path to .json or .yml file containing configuration.
289 db_url: Database URL string (e.g., "sqlite://:memory:").
290 modules: Dictionary mapping app labels to lists of model modules.
291 _create_db: If True, creates the database if it doesn't exist.
292 use_tz: If True, datetime fields will be timezone-aware.
293 timezone: Timezone to use, defaults to "UTC".
294 routers: List of database router paths or classes.
295 table_name_generator: Optional callable to generate table names.
296 init_connections: If False, skips initializing connection clients while still
297 loading apps and validating connection names against the config.
298 _enable_global_fallback: If True, sets this context as the global fallback
299 for cross-task access (e.g., asgi-lifespan scenarios). Default is False.
300
301 Raises:
302 ConfigurationError: If configuration is invalid or incomplete.
303 """
304 from tortoise.apps import Apps
305
306 # Handle config_file: load it as config dict
307 if config_file is not None:
308 if config is not None:
309 raise ConfigurationError("Cannot specify both 'config' and 'config_file'")
310 config = self._get_config_from_config_file(config_file)
311
312 # Convert input to TortoiseConfig for typed access
313 typed_config: TortoiseConfig
314 if config is None:
315 if db_url is None or modules is None:
316 raise ConfigurationError(

Calls 12

_init_timezoneMethod · 0.95
_init_routersMethod · 0.95
ConfigurationErrorClass · 0.90
generate_configFunction · 0.90
AppsClass · 0.90
set_global_contextFunction · 0.85
_initMethod · 0.80
_init_configMethod · 0.80
keysMethod · 0.80
from_dictMethod · 0.45
to_dictMethod · 0.45