Changes between Initial Version and Version 1 of Ticket #32563
- Timestamp:
- Mar 17, 2021, 9:22:13 AM (4 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #32563 – Description
initial v1 34 34 {{{ 35 35 [...] 36 def set(self, objs, *, bulk=True, clear=False):36 def set(self, objs, *, clear=False, through_defaults=None): 37 37 # Force evaluation of `objs` in case it's a queryset whose value 38 38 # could be affected by `manager.clear()`. Refs #19816. 39 39 objs = tuple(objs) 40 40 41 if self.field.null: 42 db = router.db_for_write(self.model, instance=self.instance) 43 with transaction.atomic(using=db, savepoint=False): 44 if clear: 45 self.clear(bulk=bulk) 46 self.add(*objs, bulk=bulk) 47 else: 48 old_objs = set(self.using(db).all()) 49 new_objs = [] 50 for obj in objs: 51 if obj in old_objs: 52 old_objs.remove(obj) 53 else: 54 new_objs.append(obj) 41 db = router.db_for_write(self.through, instance=self.instance) 42 with transaction.atomic(using=db, savepoint=False): 43 if clear: 44 self.clear() 45 self.add(*objs, through_defaults=through_defaults) 46 else: 47 old_ids = set(self.using(db).values_list(self.target_field.target_field.attname, flat=True)) 55 48 56 self.remove(*old_objs, bulk=bulk) 57 self.add(*new_objs, bulk=bulk) 58 else: 59 self.add(*objs, bulk=bulk) 49 new_objs = [] 50 for obj in objs: 51 fk_val = ( 52 self.target_field.get_foreign_related_value(obj)[0] 53 if isinstance(obj, self.model) else obj 54 ) 55 if fk_val in old_ids: 56 old_ids.remove(fk_val) 57 else: 58 new_objs.append(obj) 59 60 self.remove(*old_ids) 61 self.add(*new_objs, through_defaults=through_defaults) 60 62 set.alters_data = True 61 63 [...]