Performs a bulk create in one database session to speed up the process. Allows you to create multiple objects at once. A valid list of `Model` objects needs to be passed. Bulk operations do not send signals. :param objects: list of ormar models already in
(self, objects: list["T"])
| 1363 | return instance |
| 1364 | |
| 1365 | async def bulk_create(self, objects: list["T"]) -> None: |
| 1366 | """ |
| 1367 | Performs a bulk create in one database session to speed up the process. |
| 1368 | |
| 1369 | Allows you to create multiple objects at once. |
| 1370 | |
| 1371 | A valid list of `Model` objects needs to be passed. |
| 1372 | |
| 1373 | Bulk operations do not send signals. |
| 1374 | |
| 1375 | :param objects: list of ormar models already initialized and ready to save. |
| 1376 | :type objects: list[Model] |
| 1377 | """ |
| 1378 | |
| 1379 | if not objects: |
| 1380 | raise ModelListEmptyError("Bulk create objects are empty!") |
| 1381 | |
| 1382 | ready_objects = [] |
| 1383 | for i, obj in enumerate(objects): |
| 1384 | ready_objects.append(obj.prepare_model_to_save(obj.model_dump())) |
| 1385 | if i % 100 == 99: # pragma: no cover |
| 1386 | await asyncio.sleep(0) |
| 1387 | |
| 1388 | # don't use execute_many, as in databases it's executed in a loop |
| 1389 | # instead of using execute_many from drivers |
| 1390 | expr = self.table.insert().values(ready_objects) |
| 1391 | async with self.model_config.database.get_query_executor() as executor: |
| 1392 | await executor.execute(expr) |
| 1393 | |
| 1394 | for obj in objects: |
| 1395 | obj.set_save_status(True) |
| 1396 | |
| 1397 | async def bulk_update( # noqa: CCR001 |
| 1398 | self, objects: list["T"], columns: Optional[list[str]] = None |