MCPcopy
hub / github.com/django/django / add

Method add

django/db/models/fields/related_descriptors.py:845–880  ·  view source on GitHub ↗
(self, *objs, bulk=True)

Source from the content-addressed store, hash-verified

843 return queryset, rel_obj_attr, instance_attr, False, cache_name, False
844
845 def add(self, *objs, bulk=True):
846 self._check_fk_val()
847 self._remove_prefetched_objects()
848 db = router.db_for_write(self.model, instance=self.instance)
849
850 def check_and_update_obj(obj):
851 if not isinstance(obj, self.model):
852 raise TypeError(
853 "'%s' instance expected, got %r"
854 % (
855 self.model._meta.object_name,
856 obj,
857 )
858 )
859 setattr(obj, self.field.name, self.instance)
860
861 if bulk:
862 pks = []
863 for obj in objs:
864 check_and_update_obj(obj)
865 if obj._state.adding or obj._state.db != db:
866 raise ValueError(
867 "%r instance isn't saved. Use bulk=False or save "
868 "the object first." % obj
869 )
870 pks.append(obj.pk)
871 self.model._base_manager.using(db).filter(pk__in=pks).update(
872 **{
873 self.field.name: self.instance,
874 }
875 )
876 else:
877 with transaction.atomic(using=db, savepoint=False):
878 for obj in objs:
879 check_and_update_obj(obj)
880 obj.save()
881
882 add.alters_data = True
883

Callers 15

setMethod · 0.95
get_group_byMethod · 0.45
get_order_byMethod · 0.45
collect_replacementsMethod · 0.45
find_ordering_nameMethod · 0.45
get_aggregationMethod · 0.45
combineMethod · 0.45
build_filterMethod · 0.45
add_qMethod · 0.45
_add_qMethod · 0.45
setup_joinsMethod · 0.45

Calls 8

_check_fk_valMethod · 0.95
db_for_writeMethod · 0.45
appendMethod · 0.45
updateMethod · 0.45
filterMethod · 0.45
usingMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected