Tortoise-ORM Quart 集成¶
我们有一个轻量级集成工具 tortoise.contrib.quart
,它有一个函数 register_tortoise
,用于在启动时设置 Tortoise-ORM,并在关闭时清理。
请注意,模块路径不能是 __main__
,因为这会根据启动点而改变。人们希望能够直接从 ASGI 运行器启动 Quart 服务,因此所有路径都需要明确。
请参阅 Quart 示例
用法¶
QUART_APP=main quart
...
Commands:
generate-schemas Populate DB with Tortoise-ORM schemas.
run Start and run a development server.
shell Open a shell within the app context.
# To generate schemas
QUART_APP=main quart generate-schemas
# To run
QUART_APP=main quart run
参考¶
-
tortoise.contrib.quart.register_tortoise(app, config=
None
, config_file=None
, db_url=None
, modules=None
, generate_schemas=False
)[source]¶ 注册
before_serving
和after_serving
钩子,在 Quart 服务中设置和关闭 Tortoise-ORM。它还注册一个 CLI 命令generate_schemas
,该命令将生成模式。你可以仅使用
config
、config_file
和(db_url, modules)
中的一个进行配置。- 参数:¶
- app¶
Quart app。
- 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 – 对于任何配置错误
- 返回类型:¶
无