| 800 | _clear.alters_data = True |
| 801 | |
| 802 | def set(self, objs, *, bulk=True, clear=False): |
| 803 | # Force evaluation of `objs` in case it's a queryset whose value |
| 804 | # could be affected by `manager.clear()`. Refs #19816. |
| 805 | objs = tuple(objs) |
| 806 | |
| 807 | db = router.db_for_write(self.model, instance=self.instance) |
| 808 | with transaction.atomic(using=db, savepoint=False): |
| 809 | if clear: |
| 810 | self.clear() |
| 811 | self.add(*objs, bulk=bulk) |
| 812 | else: |
| 813 | old_objs = set(self.using(db).all()) |
| 814 | new_objs = [] |
| 815 | for obj in objs: |
| 816 | if obj in old_objs: |
| 817 | old_objs.remove(obj) |
| 818 | else: |
| 819 | new_objs.append(obj) |
| 820 | |
| 821 | self.remove(*old_objs) |
| 822 | self.add(*new_objs, bulk=bulk) |
| 823 | |
| 824 | set.alters_data = True |
| 825 | |