Fixed #22047 -- detecting related_name clash with inheritance

Thanks to mondone for fruitful colaboration.
This commit is contained in:
Christopher Medrela 2014-02-15 15:36:07 +01:00 committed by Baptiste Mispelon
parent 65b421c661
commit d0133504e5
2 changed files with 30 additions and 1 deletions

View File

@ -183,7 +183,7 @@ class RelatedField(Field):
# Check clashes between accessor or reverse query name of `field`
# and any other field name -- i. e. accessor for Model.foreign is
# model_set and it clashes with Target.model_set.
potential_clashes = rel_opts.fields + rel_opts.local_many_to_many
potential_clashes = rel_opts.fields + rel_opts.many_to_many
for clash_field in potential_clashes:
clash_name = "%s.%s" % (rel_opts.object_name,
clash_field.name) # i. e. "Target.model_set"

View File

@ -583,6 +583,35 @@ class AccessorClashTests(IsolatedModelsTestCase):
]
self.assertEqual(errors, expected)
def test_m2m_to_m2m_with_inheritance(self):
""" Ref #22047. """
class Target(models.Model):
pass
class Model(models.Model):
children = models.ManyToManyField('Child',
related_name="m2m_clash", related_query_name="no_clash")
class Parent(models.Model):
m2m_clash = models.ManyToManyField('Target')
class Child(Parent):
pass
errors = Model.check()
expected = [
Error(
'Accessor for field Model.children clashes with field Child.m2m_clash.',
hint=('Rename field Child.m2m_clash or add/change '
'a related_name argument to the definition '
'for field Model.children.'),
obj=Model._meta.get_field('children'),
id='E014',
)
]
self.assertEqual(errors, expected)
class ReverseQueryNameClashTests(IsolatedModelsTestCase):