From 4f6d2a1d088ae724d4261cefcd1bc273d95ab573 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 24 Feb 2006 10:28:46 +0000 Subject: [PATCH] magic-removal: Refs #1346 -- Added the production of 'mirror' entries in the m2m table for m2m relations to self. git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2382 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/related.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 3faba083b2..a7fc3539bd 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -300,15 +300,32 @@ class ReverseManyRelatedObjectsDescriptor(object): def add(self, *objs, **kwargs): _add_m2m_items(self, superclass, rel_model, join_table, source_col_name, target_col_name, instance._get_pk_val(), *objs, **kwargs) + + # If this is an m2m relation to self, add the mirror entry in the m2m table + if instance.__class__ == rel_model: + _add_m2m_items(self, superclass, rel_model, join_table, target_col_name, + source_col_name, instance._get_pk_val(), *objs, **kwargs) + add.alters_data = True def remove(self, *objs): _remove_m2m_items(rel_model, join_table, source_col_name, target_col_name, instance._get_pk_val(), *objs) + + # If this is an m2m relation to self, remove the mirror entry in the m2m table + if instance.__class__ == rel_model: + _remove_m2m_items(rel_model, join_table, target_col_name, + source_col_name, instance._get_pk_val(), *objs) + remove.alters_data = True def clear(self): _clear_m2m_items(join_table, source_col_name, instance._get_pk_val()) + + # If this is an m2m relation to self, clear the mirror entry in the m2m table + if instance.__class__ == rel_model: + _clear_m2m_items(join_table, target_col_name, instance._get_pk_val()) + clear.alters_data = True manager = RelatedManager()