Ticket #10237: m2m-inheritance.diff

File m2m-inheritance.diff, 2.2 KB (added by Justin Lilly, 16 years ago)

Contains a fix and regression tests for the reported bug.

  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 01f53aa..4e6c5ac 100644
    a b class ReverseManyRelatedObjectsDescriptor(object):  
    558558            model=rel_model,
    559559            core_filters={'%s__pk' % self.field.related_query_name(): instance._get_pk_val()},
    560560            instance=instance,
    561             symmetrical=(self.field.rel.symmetrical and instance.__class__ == rel_model),
     561            symmetrical=(self.field.rel.symmetrical and isinstance(instance, rel_model)),
    562562            join_table=qn(self.field.m2m_db_table()),
    563563            source_col_name=qn(self.field.m2m_column_name()),
    564564            target_col_name=qn(self.field.m2m_reverse_name())
  • tests/regressiontests/m2m_regress/models.py

    diff --git a/tests/regressiontests/m2m_regress/models.py b/tests/regressiontests/m2m_regress/models.py
    index cffc137..5484b26 100644
    a b class Entry(models.Model):  
    2626    def __unicode__(self):
    2727        return self.name
    2828
     29# Two models both inheriting from a base model with a self-referential m2m field
     30class SelfReferChild(SelfRefer):
     31    pass
     32
     33class SelfReferChildSibling(SelfRefer):
     34    pass
     35
    2936__test__ = {"regressions": """
    3037# Multiple m2m references to the same model or a different model must be
    3138# distinguished when accessing the relations through an instance attribute.
    __test__ = {"regressions": """  
    5764>>> SelfRefer.objects.filter(porcupine='fred')
    5865Traceback (most recent call last):
    5966...
    60 FieldError: Cannot resolve keyword 'porcupine' into field. Choices are: id, name, references, related
     67FieldError: Cannot resolve keyword 'porcupine' into field. Choices are: id, name, references, related, selfreferchild, selfreferchildsibling
     68
     69# Test to ensure that the relationship between two inherited models
     70# with a self-referential m2m field maintains symmetry
     71>>> sr_child = SelfReferChild(name="Hanna")
     72>>> sr_child.save()
     73
     74>>> sr_sibling = SelfReferChildSibling(name="Beth")
     75>>> sr_sibling.save()
     76>>> sr_child.related.add(sr_sibling)
     77>>> sr_child.related.all()
     78[<SelfRefer: Beth>]
     79>>> sr_sibling.related.all()
     80[<SelfRefer: Hanna>]
    6181
    6282"""
    6383}
Back to Top