Sanic二十七:Sanic + tortoise-orm 之Q对象 批量写入数据
- tortoise-orm官方
- 异步 类方法 bulk_create(对象, batch_size = None, using_db = None)
- 批量插入操作将尽量保证在 DB 中创建的对象具有所有默认值和生成的字段集,但在 Python 中可能是不完整的引用
-
@cases.post("/test/", name="批量新增") async def create(case: List[models.CaseIn_Pydantic]): result = [models.Case(**c.dict(exclude_unset=True)) for c in case] try: await models.Case.bulk_create(result) # bulk_create批量插入操作 return core.Success(data=await models.Case.from_queryset(models.Case)) except Exception as e: return core.Fail(message=f"创建失败.{e}")
-
有时候需要进行复杂的查询,但是仅仅靠QuerySet提供的那些方法是完全不够的,tortoise-orm提供了Q对象,用于做复杂的查询
from tortoise.query_utils import Q
1、与:&
若不写连接符,则默认为and
2、或:|
3、非:~