Ticket #5390: related.field_name.diff

File related.field_name.diff, 4.3 KB (added by murkt, 8 years ago)

related.diff with added field_name parameter (signals and dispatcher are already imported)

  • db/models/fields/related.py

     
    266266    """Creates a manager that subclasses 'superclass' (which is a Manager)
    267267    and adds behavior for many-to-many related objects."""
    268268    class ManyRelatedManager(superclass):
    269         def __init__(self, model=None, core_filters=None, instance=None, symmetrical=None,
    270                 join_table=None, source_col_name=None, target_col_name=None):
     269        def __init__(self, model=None, core_filters=None, instance=None,
     270                     symmetrical=None, join_table=None, source_col_name=None,
     271                     target_col_name=None, field_name=None):
    271272            super(ManyRelatedManager, self).__init__()
    272273            self.core_filters = core_filters
    273274            self.model = model
     
    277278            self.source_col_name = source_col_name
    278279            self.target_col_name = target_col_name
    279280            self._pk_val = self.instance._get_pk_val()
     281            self.field_name = field_name
    280282            if self._pk_val is None:
    281283                raise ValueError("%r instance needs to have a primary key value before a many-to-many relationship can be used." % model)
    282284
     
    339341                existing_ids = set([row[0] for row in cursor.fetchall()])
    340342
    341343                # Add the ones that aren't there already
    342                 for obj_id in (new_ids - existing_ids):
     344                new_ids = new_ids - existing_ids
     345                for obj_id in new_ids:
    343346                    cursor.execute("INSERT INTO %s (%s, %s) VALUES (%%s, %%s)" % \
    344347                        (self.join_table, source_col_name, target_col_name),
    345348                        [self._pk_val, obj_id])
     349
     350                added_objs = [obj for obj in objs if (isinstance(obj, self.model) and obj._get_pk_val() in new_ids) or obj in new_ids]
     351                dispatcher.send(signal=signals.m2m_add_items, sender=self.model,
     352                                instance=self.instance, field_name=self.field_name,
     353                                objs=added_objs)
     354
    346355                transaction.commit_unless_managed()
    347356
    348357        def _remove_items(self, source_col_name, target_col_name, *objs):
     
    365374                    (self.join_table, source_col_name,
    366375                    target_col_name, ",".join(['%s'] * len(old_ids))),
    367376                    [self._pk_val] + list(old_ids))
     377                dispatcher.send(signal=signals.m2m_remove_items, sender=self.model,
     378                                instance=self.instance, field_name=self.field_name,
     379                                objs=objs)
    368380                transaction.commit_unless_managed()
    369381
    370382        def _clear_items(self, source_col_name):
    371383            # source_col_name: the PK colname in join_table for the source object
     384            dispatcher.send(signal=signals.m2m_clear_items, sender=self.model,
     385                            instance=self.instance, field_name=self.field_name)
    372386            cursor = connection.cursor()
    373387            cursor.execute("DELETE FROM %s WHERE %s = %%s" % \
    374388                (self.join_table, source_col_name),
     
    405419            symmetrical=False,
    406420            join_table=qn(self.related.field.m2m_db_table()),
    407421            source_col_name=qn(self.related.field.m2m_reverse_name()),
    408             target_col_name=qn(self.related.field.m2m_column_name())
     422            target_col_name=qn(self.related.field.m2m_column_name()),
     423            field_name=self.related.field.name
    409424        )
    410425
    411426        return manager
     
    446461            symmetrical=(self.field.rel.symmetrical and instance.__class__ == rel_model),
    447462            join_table=qn(self.field.m2m_db_table()),
    448463            source_col_name=qn(self.field.m2m_column_name()),
    449             target_col_name=qn(self.field.m2m_reverse_name())
     464            target_col_name=qn(self.field.m2m_reverse_name()),
     465            field_name=self.field.name
    450466        )
    451467
    452468        return manager
  • db/models/signals.py

     
    1010post_delete = object()
    1111
    1212post_syncdb = object()
     13
     14m2m_add_items = object()
     15m2m_remove_items = object()
     16m2m_clear_items = object()
Back to Top