fastapi-tortoise-orm批量插入数据库

  • 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}")
       
    •   

       

原文地址:https://www.cnblogs.com/cheng10/p/14866831.html