Tortoise 经验

一、增删查改

复制代码
    user = {"id": 2, "mobile": "2313", "name": "2313"}
    company = {"id": 2, "name": "2313", "creator_id": 2313}
    match = {'id': 6}
    values = {'iint': 7, 'sstr': '2443349403', 'user': user, 'company': company, 'id': 3}
    start_time = time.time()
    ts = await Test.all().values()
    ts = await Test.filter(**match).update(**values)
    ts = await Test.create(**values)
    crate_time = time.time() - start_time
复制代码

二、连接设置

复制代码
config = {
    'connections': {
        # Dict format for connection
        'default': {
            'engine': 'tortoise.backends.asyncpg',
            'credentials': {
                'host': 'localhost',
                'port': '5432',
                'user': 'postgres',
                'password': 'Emi,/****',
                'database': 'xy',
            }
        },
        # Using a DB_URL string
        # 'default': 'postgres://postgres:qwerty123@localhost:5432/events'
    },
    'apps': {
        'models': {
            'models': ['models'],    # models.py 文件跟 main.py 在同一目录内
            # If no default_connection specified, defaults to 'default'   __main__
            'default_connection': 'default',
        }
    }
}

register_tortoise(
    app,
    config=config,
    config_file=None,
    db_url=None,
    modules={"models": ["models"]},
    generate_schemas=False,
    add_exception_handlers=False
)
复制代码

 

posted @   pearlcity  阅读(113)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示