(self, objs, *, bulk=True, clear=False)
| 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 |
no test coverage detected