MCPcopy
hub / github.com/django/django / set

Method set

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

Source from the content-addressed store, hash-verified

988 _clear.alters_data = True
989
990 def set(self, objs, *, bulk=True, clear=False):
991 self._check_fk_val()
992 # Force evaluation of `objs` in case it's a queryset whose value
993 # could be affected by `manager.clear()`. Refs #19816.
994 objs = tuple(objs)
995
996 if self.field.null:
997 db = router.db_for_write(self.model, instance=self.instance)
998 with transaction.atomic(using=db, savepoint=False):
999 if clear:
1000 self.clear(bulk=bulk)
1001 self.add(*objs, bulk=bulk)
1002 else:
1003 old_objs = set(self.using(db).all())
1004 new_objs = []
1005 for obj in objs:
1006 if obj in old_objs:
1007 old_objs.remove(obj)
1008 else:
1009 new_objs.append(obj)
1010
1011 self.remove(*old_objs, bulk=bulk)
1012 self.add(*new_objs, bulk=bulk)
1013 else:
1014 self.add(*objs, bulk=bulk)
1015
1016 set.alters_data = True
1017

Callers 2

_restore_contextFunction · 0.45
save_form_dataMethod · 0.45

Calls 8

_check_fk_valMethod · 0.95
clearMethod · 0.95
addMethod · 0.95
removeMethod · 0.95
db_for_writeMethod · 0.45
allMethod · 0.45
usingMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected