Skip to content

Commit

Permalink
[1.0.X] Fixed #10237 -- Corrected the handling of self-referential m2…
Browse files Browse the repository at this point in the history
…m fields when using multi-table inheritance. Thanks to Justin Lilly for the report and patch.

Merge of r10550 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10551 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Apr 13, 2009
1 parent cab8501 commit 2c6e3b3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion django/db/models/fields/related.py
Expand Up @@ -575,7 +575,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())
Expand Down
22 changes: 21 additions & 1 deletion tests/regressiontests/m2m_regress/models.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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>]
"""
}

0 comments on commit 2c6e3b3

Please sign in to comment.