Tortoise-ORM BlackSheep 集成¶
Tortoise-ORM BlackSheep integration
我们有一个轻量级的集成工具 tortoise.contrib.blacksheep,其中包含一个函数 register_tortoise,该函数在启动时设置 Tortoise-ORM,并在拆卸时进行清理。
BlackSheep 是一个异步网络框架,用于使用 Python 构建基于事件的 web 应用程序。
请查看 BlackSheep 样例,并参考 Pydantic 序列化 教程。
We have a lightweight integration util tortoise.contrib.blacksheep which has a single function register_tortoise which sets up Tortoise-ORM on startup and cleans up on teardown.
BlackSheep is an asynchronous web framework to build event based web applications with Python.
See the BlackSheep 样例 & have a look at the Pydantic 序列化 tutorials.
参考¶
Reference
-
tortoise.contrib.blacksheep.register_tortoise(app, config=
None, config_file=None, db_url=None, modules=None, generate_schemas=False, add_exception_handlers=False)[source]¶ Registers
startupandshutdownevents to set-up and tear-down Tortoise-ORM inside a BlackSheep application.You can configure using only one of
config,config_fileand(db_url, modules).- Parameters:¶
- app¶
BlackSheep app.
- config=
None¶ Dict containing config:
Example
{ 'connections': { # Dict format for connection 'default': { 'engine': 'tortoise.backends.asyncpg', 'credentials': { 'host': 'localhost', 'port': '5432', 'user': 'tortoise', 'password': 'qwerty123', 'database': 'test', } }, # Using a DB_URL string 'default': 'postgres://postgres:qwerty123@localhost:5432/events' }, 'apps': { 'models': { 'models': ['__main__'], # If no default_connection specified, defaults to 'default' 'default_connection': 'default', } } }- config_file=
None¶ Path to .json or .yml (if PyYAML installed) file containing config with same format as above.
- db_url=
None¶ Use a DB_URL string. See DB_URL
- modules=
None¶ Dictionary of
key: [list_of_modules] that defined “apps” and modules that should be discovered for models.- generate_schemas=
False¶ True to generate schema immediately. Only useful for dev environments or SQLite
:memory:databases- add_exception_handlers=
False¶ True to add some automatic exception handlers for
DoesNotExist&IntegrityError. This is not recommended for production systems as it may leak data.
- Raises:¶
ConfigurationError – For any configuration error
- Return type:¶
None