Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #10237 -- Corrected the handling of self-referential m2m fields…

… when using multi-table inheritance. Thanks to Justin Lilly for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10550 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0fec0a5d704d487755209115719ca173913a43b7 1 parent ffd7b17
Russell Keith-Magee freakboy3742 authored
2  django/db/models/fields/related.py
View
@@ -582,7 +582,7 @@ def __get__(self, instance, instance_type=None):
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())
22 tests/regressiontests/m2m_regress/models.py
View
@@ -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 @@ def __unicode__(self):
>>> 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()
+[<SelfRefer: Beth>]
+>>> sr_sibling.related.all()
+[<SelfRefer: Hanna>]
"""
}
Please sign in to comment.
Something went wrong with that request. Please try again.