diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 1d083cd9879..f2723a18c76 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -582,7 +582,7 @@ class ReverseManyRelatedObjectsDescriptor(object): model=rel_model, core_filters={'%s__pk' % self.field.related_query_name(): instance._get_pk_val()}, instance=instance, - symmetrical=(self.field.rel.symmetrical and instance.__class__ == rel_model), + symmetrical=(self.field.rel.symmetrical and isinstance(instance, rel_model)), join_table=qn(self.field.m2m_db_table()), source_col_name=qn(self.field.m2m_column_name()), target_col_name=qn(self.field.m2m_reverse_name()) diff --git a/tests/regressiontests/m2m_regress/models.py b/tests/regressiontests/m2m_regress/models.py index cffc137e70b..5484b26d172 100644 --- a/tests/regressiontests/m2m_regress/models.py +++ b/tests/regressiontests/m2m_regress/models.py @@ -26,6 +26,13 @@ class Entry(models.Model): def __unicode__(self): return self.name +# Two models both inheriting from a base model with a self-referential m2m field +class SelfReferChild(SelfRefer): + pass + +class SelfReferChildSibling(SelfRefer): + pass + __test__ = {"regressions": """ # Multiple m2m references to the same model or a different model must be # distinguished when accessing the relations through an instance attribute. @@ -57,7 +64,20 @@ __test__ = {"regressions": """ >>> SelfRefer.objects.filter(porcupine='fred') Traceback (most recent call last): ... -FieldError: Cannot resolve keyword 'porcupine' into field. Choices are: id, name, references, related +FieldError: Cannot resolve keyword 'porcupine' into field. Choices are: id, name, references, related, selfreferchild, selfreferchildsibling + +# Test to ensure that the relationship between two inherited models +# with a self-referential m2m field maintains symmetry +>>> sr_child = SelfReferChild(name="Hanna") +>>> sr_child.save() + +>>> sr_sibling = SelfReferChildSibling(name="Beth") +>>> sr_sibling.save() +>>> sr_child.related.add(sr_sibling) +>>> sr_child.related.all() +[] +>>> sr_sibling.related.all() +[] """ }