MCPcopy
hub / github.com/django/django / bulk_create

Method bulk_create

django/db/models/query.py:794–897  ·  view source on GitHub ↗

Insert each of the instances into the database. Do *not* call save() on each of the instances, do not send any pre/post_save signals, and do not set the primary key attribute if it is an autoincrement field (except if features.can_return_rows_from_bulk_insert

(
        self,
        objs,
        batch_size=None,
        ignore_conflicts=False,
        update_conflicts=False,
        update_fields=None,
        unique_fields=None,
    )

Source from the content-addressed store, hash-verified

792 return None
793
794 def bulk_create(
795 self,
796 objs,
797 batch_size=None,
798 ignore_conflicts=False,
799 update_conflicts=False,
800 update_fields=None,
801 unique_fields=None,
802 ):
803 """
804 Insert each of the instances into the database. Do *not* call
805 save() on each of the instances, do not send any pre/post_save
806 signals, and do not set the primary key attribute if it is an
807 autoincrement field (except if
808 features.can_return_rows_from_bulk_insert=True).
809 Multi-table models are not supported.
810 """
811 # When you bulk insert you don't get the primary keys back (if it's an
812 # autoincrement, except if can_return_rows_from_bulk_insert=True), so
813 # you can't insert into the child tables which references this. There
814 # are two workarounds:
815 # 1) This could be implemented if you didn't have an autoincrement pk
816 # 2) You could do it by doing O(n) normal inserts into the parent
817 # tables to get the primary keys back and then doing a single bulk
818 # insert into the childmost table.
819 # We currently set the primary keys on the objects when using
820 # PostgreSQL via the RETURNING ID clause. It should be possible for
821 # Oracle as well, but the semantics for extracting the primary keys is
822 # trickier so it's not done yet.
823 if batch_size is not None and batch_size <= 0:
824 raise ValueError("Batch size must be a positive integer.")
825 # Check that the parents share the same concrete model with the our
826 # model to detect the inheritance pattern ConcreteGrandParent ->
827 # MultiTableParent -> ProxyChild. Simply checking
828 # self.model._meta.proxy would not identify that case as involving
829 # multiple tables.
830 for parent in self.model._meta.all_parents:
831 if parent._meta.concrete_model is not self.model._meta.concrete_model:
832 raise ValueError("Can't bulk create a multi-table inherited model")
833 if not objs:
834 return objs
835 opts = self.model._meta
836 if unique_fields:
837 # Primary key is allowed in unique_fields.
838 unique_fields = [
839 self.model._meta.get_field(opts.pk.name if name == "pk" else name)
840 for name in unique_fields
841 ]
842 if update_fields:
843 update_fields = [self.model._meta.get_field(name) for name in update_fields]
844 on_conflict = self._check_bulk_create_options(
845 ignore_conflicts,
846 update_conflicts,
847 update_fields,
848 unique_fields,
849 )
850 self._for_write = True
851 fields = [f for f in opts.concrete_fields if not f.generated]

Callers 15

create_permissionsFunction · 0.80
create_contenttypesFunction · 0.80
log_actionsMethod · 0.80
_add_itemsMethod · 0.80
setUpTestDataMethod · 0.80
setUpTestDataMethod · 0.80
setUpTestDataMethod · 0.80
setUpTestDataMethod · 0.80
setUpTestDataMethod · 0.80
setUpTestDataMethod · 0.80
setUpTestDataMethod · 0.80
setUpTestDataMethod · 0.80

Calls 5

_batched_insertMethod · 0.95
get_fieldMethod · 0.45

Tested by 15

setUpTestDataMethod · 0.64
setUpTestDataMethod · 0.64
setUpTestDataMethod · 0.64
setUpTestDataMethod · 0.64
setUpTestDataMethod · 0.64
setUpTestDataMethod · 0.64
setUpTestDataMethod · 0.64
setUpTestDataMethod · 0.64
setUpTestDataMethod · 0.64
test_get_paginatorMethod · 0.64
test_selectMethod · 0.64