Ticket #1334: magic_removal-manytomany_save_fix.diff

File magic_removal-manytomany_save_fix.diff, 2.4 KB (added by Tom Tobin <korpios@…>, 19 years ago)

fixes save method of AutomaticManipulator

  • db/models/manipulators.py

     
    88from django.utils.datastructures import DotExpandedDict, MultiValueDict
    99import types
    1010
     11# For Python 2.3
     12if not hasattr(__builtins__, 'set'):
     13    from sets import Set as set
     14
    1115def add_manipulators(sender):
    1216    cls = sender
    1317    cls.add_to_class('AddManipulator', AutomaticAddManipulator)
     
    119123                        new_vals = new_data.get(f.name, ())
    120124                    else:
    121125                        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))
    124141                        self.fields_changed.append(f.verbose_name)
    125142
    126143        expanded_data = DotExpandedDict(dict(new_data))
  • db/models/fields/related.py

     
    127127        (rel_col_name, join_table, this_col_name,
    128128        rel_col_name, ",".join(['%s'] * len(new_ids))),
    129129        [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()
    131134
    132135    # Add the ones that aren't there already
    133136    for obj_id in (new_ids - existing_ids):
Back to Top