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

Method add

django/contrib/contenttypes/fields.py:722–756  ·  view source on GitHub ↗
(self, *objs, bulk=True)

Source from the content-addressed store, hash-verified

720 )
721
722 def add(self, *objs, bulk=True):
723 self._remove_prefetched_objects()
724 db = router.db_for_write(self.model, instance=self.instance)
725
726 def check_and_update_obj(obj):
727 if not isinstance(obj, self.model):
728 raise TypeError(
729 "'%s' instance expected, got %r"
730 % (self.model._meta.object_name, obj)
731 )
732 setattr(obj, self.content_type_field_name, self.content_type)
733 setattr(obj, self.object_id_field_name, self.pk_val)
734
735 if bulk:
736 pks = []
737 for obj in objs:
738 if obj._state.adding or obj._state.db != db:
739 raise ValueError(
740 "%r instance isn't saved. Use bulk=False or save "
741 "the object first." % obj
742 )
743 check_and_update_obj(obj)
744 pks.append(obj.pk)
745
746 self.model._base_manager.using(db).filter(pk__in=pks).update(
747 **{
748 self.content_type_field_name: self.content_type,
749 self.object_id_field_name: self.pk_val,
750 }
751 )
752 else:
753 with transaction.atomic(using=db, savepoint=False):
754 for obj in objs:
755 check_and_update_obj(obj)
756 obj.save()
757
758 add.alters_data = True
759

Callers 3

setMethod · 0.95
get_for_modelsMethod · 0.45

Calls 7

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