Ticket #1334: magic_removal-manytomany_save_fix.diff
File magic_removal-manytomany_save_fix.diff, 2.4 KB (added by , 19 years ago) |
---|
-
db/models/manipulators.py
8 8 from django.utils.datastructures import DotExpandedDict, MultiValueDict 9 9 import types 10 10 11 # For Python 2.3 12 if not hasattr(__builtins__, 'set'): 13 from sets import Set as set 14 11 15 def add_manipulators(sender): 12 16 cls = sender 13 17 cls.add_to_class('AddManipulator', AutomaticAddManipulator) … … 119 123 new_vals = new_data.get(f.name, ()) 120 124 else: 121 125 new_vals = new_data.getlist(f.name) 122 was_changed = getattr(new_object, 'set_%s' % f.name)(new_vals) 123 if self.change and was_changed: 126 try: 127 new_vals = [int(x) for x in new_vals] 128 except ValueError: 129 pass 130 rel_manager = getattr(new_object, f.name) 131 prev_vals = [x._get_pk_val() for x in rel_manager.all()] 132 prev_val_set = set(prev_vals) 133 new_val_set = set(new_vals) 134 if self.change and prev_val_set != new_val_set: 135 remove_val_set = prev_val_set - new_val_set 136 add_val_set = new_val_set - prev_val_set 137 if remove_val_set: 138 rel_manager.remove(*rel_manager.model.objects.filter(id__in=remove_val_set)) 139 if add_val_set: 140 rel_manager.add(*rel_manager.model.objects.filter(id__in=add_val_set)) 124 141 self.fields_changed.append(f.verbose_name) 125 142 126 143 expanded_data = DotExpandedDict(dict(new_data)) -
db/models/fields/related.py
127 127 (rel_col_name, join_table, this_col_name, 128 128 rel_col_name, ",".join(['%s'] * len(new_ids))), 129 129 [this_pk_val] + list(new_ids)) 130 existing_ids = set([row[0] for row in cursor.fetchmany(rowcount)]) 130 if rowcount: 131 existing_ids = set([row[0] for row in cursor.fetchmany(rowcount)]) 132 else: 133 existing_ids = set() 131 134 132 135 # Add the ones that aren't there already 133 136 for obj_id in (new_ids - existing_ids):