Tortoise-ORM aiohttp 集成¶
我们有一个轻量级集成工具 tortoise.contrib.aiohttp
,它有一个单一函数 register_tortoise
,它在启动时设置 Tortoise-ORM,并在关闭时清理。
参见 AIOHTTP 示例
参考¶
-
tortoise.contrib.aiohttp.register_tortoise(app, config=
None
, config_file=None
, db_url=None
, modules=None
, generate_schemas=False
)[源代码]¶ 注册
on_startup
和on_shutdown
钩子,以便在 Aiohttp Web 服务器内设置和拆除 Tortoise-ORM。您只能使用
config
、config_file
和(db_url, modules)
中的一个进行配置。- 参数:¶
- app¶
Aiohttp 应用程序。
- config=
None
¶ 包含配置的字典
示例
{ '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
¶ 指向 .json 或 .yml(如果已安装 PyYAML)文件的路径,该文件包含与上述格式相同的配置。
- db_url=
None
¶ 使用 DB_URL 字符串。请参阅 DB_URL
- modules=
None
¶ 定义了“应用”和应为模型发现的模块的
key
: [list_of_modules
] 字典。- generate_schemas=
False
¶ True 表示立即生成架构。仅对开发环境或 SQLite
:memory:
数据库有用
- 引发:¶
ConfigurationError – 对于任何配置错误
- 返回类型:¶
无