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 @ 2022-09-30 20:15  pearlcity  阅读(97)  评论(0编辑  收藏  举报