MCPcopy
hub / github.com/django/django / _batched_insert

Method _batched_insert

django/db/models/query.py:2123–2166  ·  view source on GitHub ↗

Helper method for bulk_create() to insert objs one batch at a time.

(
        self,
        objs,
        fields,
        batch_size,
        on_conflict=None,
        update_fields=None,
        unique_fields=None,
    )

Source from the content-addressed store, hash-verified

2121 _insert.queryset_only = False
2122
2123 def _batched_insert(
2124 self,
2125 objs,
2126 fields,
2127 batch_size,
2128 on_conflict=None,
2129 update_fields=None,
2130 unique_fields=None,
2131 ):
2132 """
2133 Helper method for bulk_create() to insert objs one batch at a time.
2134 """
2135 connection = connections[self.db]
2136 ops = connection.ops
2137 max_batch_size = max(ops.bulk_batch_size(fields, objs), 1)
2138 batch_size = min(batch_size, max_batch_size) if batch_size else max_batch_size
2139 inserted_rows = []
2140 returning_fields = (
2141 self.model._meta.db_returning_fields
2142 if (
2143 connection.features.can_return_rows_from_bulk_insert
2144 and (on_conflict is None or on_conflict == OnConflict.UPDATE)
2145 )
2146 else None
2147 )
2148 batches = [objs[i : i + batch_size] for i in range(0, len(objs), batch_size)]
2149 if len(batches) > 1:
2150 context = transaction.atomic(using=self.db, savepoint=False)
2151 else:
2152 context = nullcontext()
2153 with context:
2154 for item in batches:
2155 inserted_rows.extend(
2156 self._insert(
2157 item,
2158 fields=fields,
2159 using=self.db,
2160 on_conflict=on_conflict,
2161 update_fields=update_fields,
2162 unique_fields=unique_fields,
2163 returning_fields=returning_fields,
2164 )
2165 )
2166 return inserted_rows
2167
2168 def _disable_cloning(self):
2169 """

Callers 1

bulk_createMethod · 0.95

Calls 3

_insertMethod · 0.95
bulk_batch_sizeMethod · 0.80
extendMethod · 0.80

Tested by

no test coverage detected