Code

Ticket #5390: related_manager.2.diff

File related_manager.2.diff, 2.9 KB (added by ludo@…, 7 years ago)

better patch, checked against django regression tests

Line 
1Index: db/models/signals.py
2===================================================================
3--- db/models/signals.py        (revision 6087)
4+++ db/models/signals.py        (working copy)
5@@ -10,3 +10,9 @@
6 post_delete = object()
7 
8 post_syncdb = object()
9+
10+m2m_add_items = object()
11+
12+m2m_remove_items = object()
13+
14+m2m_clear_items = object()
15Index: db/models/fields/related.py
16===================================================================
17--- db/models/fields/related.py (revision 6087)
18+++ db/models/fields/related.py (working copy)
19@@ -2,6 +2,8 @@
20 from django.db.models import signals, get_model
21 from django.db.models.fields import AutoField, Field, IntegerField, PositiveIntegerField, PositiveSmallIntegerField, get_ul_class
22 from django.db.models.related import RelatedObject
23+from django.db.models import signals
24+from django.dispatch import dispatcher
25 from django.utils.text import capfirst
26 from django.utils.translation import ugettext_lazy, string_concat, ungettext, ugettext as _
27 from django.utils.functional import curry
28@@ -339,10 +341,16 @@
29                 existing_ids = set([row[0] for row in cursor.fetchall()])
30 
31                 # Add the ones that aren't there already
32-                for obj_id in (new_ids - existing_ids):
33+                new_ids = new_ids - existing_ids
34+                for obj_id in new_ids:
35                     cursor.execute("INSERT INTO %s (%s, %s) VALUES (%%s, %%s)" % \
36                         (self.join_table, source_col_name, target_col_name),
37                         [self._pk_val, obj_id])
38+                       
39+                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]
40+                dispatcher.send(signal=signals.m2m_add_items, sender=self.model, added=added_objs,
41+                    source_col_name=source_col_name, target_col_name=target_col_name, instance=self.instance)
42+                       
43                 transaction.commit_unless_managed()
44 
45         def _remove_items(self, source_col_name, target_col_name, *objs):
46@@ -365,6 +373,8 @@
47                     (self.join_table, source_col_name,
48                     target_col_name, ",".join(['%s'] * len(old_ids))),
49                     [self._pk_val] + list(old_ids))
50+                dispatcher.send(signal=signals.m2m_remove_items, sender=self.model, added=objs,
51+                    source_col_name=source_col_name, target_col_name=target_col_name, instance=self.instance)
52                 transaction.commit_unless_managed()
53 
54         def _clear_items(self, source_col_name):
55@@ -373,6 +383,8 @@
56             cursor.execute("DELETE FROM %s WHERE %s = %%s" % \
57                 (self.join_table, source_col_name),
58                 [self._pk_val])
59+            dispatcher.send(signal=signals.m2m_clear_items, sender=self.model,
60+                source_col_name=source_col_name, instance=self.instance)
61             transaction.commit_unless_managed()
62 
63     return ManyRelatedManager