Fixed #33131 -- Improved error messages for clashing reverse accessor names.

This commit is contained in:
Bernd Wechner 2021-09-22 15:53:10 +10:00 committed by Mariusz Felisiak
parent 402ae37873
commit 2116238d5f
4 changed files with 72 additions and 60 deletions

View File

@ -239,7 +239,9 @@ class RelatedField(FieldCacheMixin, Field):
if not rel_is_hidden and clash_field.name == rel_name: if not rel_is_hidden and clash_field.name == rel_name:
errors.append( errors.append(
checks.Error( checks.Error(
"Reverse accessor for '%s' clashes with field name '%s'." % (field_name, clash_name), f"Reverse accessor '{rel_opts.object_name}.{rel_name}' "
f"for '{field_name}' clashes with field name "
f"'{clash_name}'.",
hint=("Rename field '%s', or add/change a related_name " hint=("Rename field '%s', or add/change a related_name "
"argument to the definition for field '%s'.") % (clash_name, field_name), "argument to the definition for field '%s'.") % (clash_name, field_name),
obj=self, obj=self,
@ -271,7 +273,9 @@ class RelatedField(FieldCacheMixin, Field):
if not rel_is_hidden and clash_field.get_accessor_name() == rel_name: if not rel_is_hidden and clash_field.get_accessor_name() == rel_name:
errors.append( errors.append(
checks.Error( checks.Error(
"Reverse accessor for '%s' clashes with reverse accessor for '%s'." % (field_name, clash_name), f"Reverse accessor '{rel_opts.object_name}.{rel_name}' "
f"for '{field_name}' clashes with reverse accessor for "
f"'{clash_name}'.",
hint=("Add or change a related_name argument " hint=("Add or change a related_name argument "
"to the definition for '%s' or '%s'.") % (field_name, clash_name), "to the definition for '%s' or '%s'.") % (field_name, clash_name),
obj=self, obj=self,

View File

@ -247,12 +247,14 @@ Related fields
either not installed, or is abstract. either not installed, or is abstract.
* **fields.E301**: Field defines a relation with the model * **fields.E301**: Field defines a relation with the model
``<app_label>.<model>`` which has been swapped out. ``<app_label>.<model>`` which has been swapped out.
* **fields.E302**: Reverse accessor for ``<app_label>.<model>.<field name>`` * **fields.E302**: Reverse accessor ``<related model>.<accessor name>`` for
clashes with field name ``<app_label>.<model>.<field name>``. ``<app_label>.<model>.<field name>`` clashes with field name
``<app_label>.<model>.<field name>``.
* **fields.E303**: Reverse query name for ``<app_label>.<model>.<field name>`` * **fields.E303**: Reverse query name for ``<app_label>.<model>.<field name>``
clashes with field name ``<app_label>.<model>.<field name>``. clashes with field name ``<app_label>.<model>.<field name>``.
* **fields.E304**: Reverse accessor for ``<app_label>.<model>.<field name>`` * **fields.E304**: Reverse accessor ``<related model>.<accessor name>`` for
clashes with reverse accessor for ``<app_label>.<model>.<field name>``. ``<app_label>.<model>.<field name>`` clashes with reverse accessor for
``<app_label>.<model>.<field name>``.
* **fields.E305**: Reverse query name for ``<app_label>.<model>.<field name>`` * **fields.E305**: Reverse query name for ``<app_label>.<model>.<field name>``
clashes with reverse query name for ``<app_label>.<model>.<field name>``. clashes with reverse query name for ``<app_label>.<model>.<field name>``.
* **fields.E306**: The name ``<name>`` is invalid ``related_name`` for field * **fields.E306**: The name ``<name>`` is invalid ``related_name`` for field

View File

@ -862,8 +862,8 @@ class AccessorClashTests(SimpleTestCase):
self.assertEqual(Model.check(), [ self.assertEqual(Model.check(), [
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.rel' " "Reverse accessor 'Target.model_set' for "
"clashes with field name " "'invalid_models_tests.Model.rel' clashes with field name "
"'invalid_models_tests.Target.model_set'.", "'invalid_models_tests.Target.model_set'.",
hint=( hint=(
"Rename field 'invalid_models_tests.Target.model_set', or " "Rename field 'invalid_models_tests.Target.model_set', or "
@ -885,9 +885,9 @@ class AccessorClashTests(SimpleTestCase):
self.assertEqual(Model.check(), [ self.assertEqual(Model.check(), [
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.foreign' " "Reverse accessor 'Target.model_set' for "
"clashes with reverse accessor for " "'invalid_models_tests.Model.foreign' clashes with reverse "
"'invalid_models_tests.Model.m2m'.", "accessor for 'invalid_models_tests.Model.m2m'.",
hint=( hint=(
"Add or change a related_name argument to the definition " "Add or change a related_name argument to the definition "
"for 'invalid_models_tests.Model.foreign' or " "for 'invalid_models_tests.Model.foreign' or "
@ -897,9 +897,9 @@ class AccessorClashTests(SimpleTestCase):
id='fields.E304', id='fields.E304',
), ),
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.m2m' " "Reverse accessor 'Target.model_set' for "
"clashes with reverse accessor for " "'invalid_models_tests.Model.m2m' clashes with reverse "
"'invalid_models_tests.Model.foreign'.", "accessor for 'invalid_models_tests.Model.foreign'.",
hint=( hint=(
"Add or change a related_name argument to the definition " "Add or change a related_name argument to the definition "
"for 'invalid_models_tests.Model.m2m' or " "for 'invalid_models_tests.Model.m2m' or "
@ -927,9 +927,9 @@ class AccessorClashTests(SimpleTestCase):
self.assertEqual(Model.check(), [ self.assertEqual(Model.check(), [
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.children' " "Reverse accessor 'Child.m2m_clash' for "
"clashes with field name " "'invalid_models_tests.Model.children' clashes with field "
"'invalid_models_tests.Child.m2m_clash'.", "name 'invalid_models_tests.Child.m2m_clash'.",
hint=( hint=(
"Rename field 'invalid_models_tests.Child.m2m_clash', or " "Rename field 'invalid_models_tests.Child.m2m_clash', or "
"add/change a related_name argument to the definition for " "add/change a related_name argument to the definition for "
@ -1085,8 +1085,9 @@ class ExplicitRelatedNameClashTests(SimpleTestCase):
self.assertEqual(Model.check(), [ self.assertEqual(Model.check(), [
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.rel' " "Reverse accessor 'Target.clash' for "
"clashes with field name 'invalid_models_tests.Target.clash'.", "'invalid_models_tests.Model.rel' clashes with field name "
"'invalid_models_tests.Target.clash'.",
hint=( hint=(
"Rename field 'invalid_models_tests.Target.clash', or " "Rename field 'invalid_models_tests.Target.clash', or "
"add/change a related_name argument to the definition for " "add/change a related_name argument to the definition for "
@ -1218,9 +1219,9 @@ class SelfReferentialM2MClashTests(SimpleTestCase):
self.assertEqual(Model.check(), [ self.assertEqual(Model.check(), [
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.first_m2m' " "Reverse accessor 'Model.model_set' for "
"clashes with reverse accessor for " "'invalid_models_tests.Model.first_m2m' clashes with reverse "
"'invalid_models_tests.Model.second_m2m'.", "accessor for 'invalid_models_tests.Model.second_m2m'.",
hint=( hint=(
"Add or change a related_name argument to the definition " "Add or change a related_name argument to the definition "
"for 'invalid_models_tests.Model.first_m2m' or " "for 'invalid_models_tests.Model.first_m2m' or "
@ -1230,9 +1231,9 @@ class SelfReferentialM2MClashTests(SimpleTestCase):
id='fields.E304', id='fields.E304',
), ),
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.second_m2m' " "Reverse accessor 'Model.model_set' for "
"clashes with reverse accessor for " "'invalid_models_tests.Model.second_m2m' clashes with reverse "
"'invalid_models_tests.Model.first_m2m'.", "accessor for 'invalid_models_tests.Model.first_m2m'.",
hint=( hint=(
"Add or change a related_name argument to the definition " "Add or change a related_name argument to the definition "
"for 'invalid_models_tests.Model.second_m2m' or " "for 'invalid_models_tests.Model.second_m2m' or "
@ -1249,9 +1250,9 @@ class SelfReferentialM2MClashTests(SimpleTestCase):
self.assertEqual(Model.check(), [ self.assertEqual(Model.check(), [
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.model_set' " "Reverse accessor 'Model.model_set' for "
"clashes with field name " "'invalid_models_tests.Model.model_set' clashes with field "
"'invalid_models_tests.Model.model_set'.", "name 'invalid_models_tests.Model.model_set'.",
hint=( hint=(
"Rename field 'invalid_models_tests.Model.model_set', or " "Rename field 'invalid_models_tests.Model.model_set', or "
"add/change a related_name argument to the definition for " "add/change a related_name argument to the definition for "
@ -1287,8 +1288,9 @@ class SelfReferentialM2MClashTests(SimpleTestCase):
self.assertEqual(Model.check(), [ self.assertEqual(Model.check(), [
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.m2m' " "Reverse accessor 'Model.clash' for "
"clashes with field name 'invalid_models_tests.Model.clash'.", "'invalid_models_tests.Model.m2m' clashes with field name "
"'invalid_models_tests.Model.clash'.",
hint=( hint=(
"Rename field 'invalid_models_tests.Model.clash', or " "Rename field 'invalid_models_tests.Model.clash', or "
"add/change a related_name argument to the definition for " "add/change a related_name argument to the definition for "
@ -1327,9 +1329,9 @@ class SelfReferentialFKClashTests(SimpleTestCase):
self.assertEqual(Model.check(), [ self.assertEqual(Model.check(), [
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.model_set' " "Reverse accessor 'Model.model_set' for "
"clashes with field name " "'invalid_models_tests.Model.model_set' clashes with field "
"'invalid_models_tests.Model.model_set'.", "name 'invalid_models_tests.Model.model_set'.",
hint=( hint=(
"Rename field 'invalid_models_tests.Model.model_set', or " "Rename field 'invalid_models_tests.Model.model_set', or "
"add/change a related_name argument to the definition for " "add/change a related_name argument to the definition for "
@ -1365,8 +1367,9 @@ class SelfReferentialFKClashTests(SimpleTestCase):
self.assertEqual(Model.check(), [ self.assertEqual(Model.check(), [
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.foreign' " "Reverse accessor 'Model.clash' for "
"clashes with field name 'invalid_models_tests.Model.clash'.", "'invalid_models_tests.Model.foreign' clashes with field name "
"'invalid_models_tests.Model.clash'.",
hint=( hint=(
"Rename field 'invalid_models_tests.Model.clash', or " "Rename field 'invalid_models_tests.Model.clash', or "
"add/change a related_name argument to the definition for " "add/change a related_name argument to the definition for "
@ -1413,8 +1416,9 @@ class ComplexClashTests(SimpleTestCase):
self.assertEqual(Model.check(), [ self.assertEqual(Model.check(), [
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.foreign_1' " "Reverse accessor 'Target.id' for "
"clashes with field name 'invalid_models_tests.Target.id'.", "'invalid_models_tests.Model.foreign_1' clashes with field "
"name 'invalid_models_tests.Target.id'.",
hint=( hint=(
"Rename field 'invalid_models_tests.Target.id', or " "Rename field 'invalid_models_tests.Target.id', or "
"add/change a related_name argument to the definition for " "add/change a related_name argument to the definition for "
@ -1435,9 +1439,9 @@ class ComplexClashTests(SimpleTestCase):
id='fields.E303', id='fields.E303',
), ),
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.foreign_1' " "Reverse accessor 'Target.id' for "
"clashes with reverse accessor for " "'invalid_models_tests.Model.foreign_1' clashes with reverse "
"'invalid_models_tests.Model.m2m_1'.", "accessor for 'invalid_models_tests.Model.m2m_1'.",
hint=( hint=(
"Add or change a related_name argument to the definition " "Add or change a related_name argument to the definition "
"for 'invalid_models_tests.Model.foreign_1' or " "for 'invalid_models_tests.Model.foreign_1' or "
@ -1460,9 +1464,9 @@ class ComplexClashTests(SimpleTestCase):
), ),
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.foreign_2' " "Reverse accessor 'Target.src_safe' for "
"clashes with reverse accessor for " "'invalid_models_tests.Model.foreign_2' clashes with reverse "
"'invalid_models_tests.Model.m2m_2'.", "accessor for 'invalid_models_tests.Model.m2m_2'.",
hint=( hint=(
"Add or change a related_name argument to the definition " "Add or change a related_name argument to the definition "
"for 'invalid_models_tests.Model.foreign_2' or " "for 'invalid_models_tests.Model.foreign_2' or "
@ -1485,8 +1489,9 @@ class ComplexClashTests(SimpleTestCase):
), ),
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.m2m_1' " "Reverse accessor 'Target.id' for "
"clashes with field name 'invalid_models_tests.Target.id'.", "'invalid_models_tests.Model.m2m_1' clashes with field name "
"'invalid_models_tests.Target.id'.",
hint=( hint=(
"Rename field 'invalid_models_tests.Target.id', or " "Rename field 'invalid_models_tests.Target.id', or "
"add/change a related_name argument to the definition for " "add/change a related_name argument to the definition for "
@ -1507,9 +1512,9 @@ class ComplexClashTests(SimpleTestCase):
id='fields.E303', id='fields.E303',
), ),
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.m2m_1' " "Reverse accessor 'Target.id' for "
"clashes with reverse accessor for " "'invalid_models_tests.Model.m2m_1' clashes with reverse "
"'invalid_models_tests.Model.foreign_1'.", "accessor for 'invalid_models_tests.Model.foreign_1'.",
hint=( hint=(
"Add or change a related_name argument to the definition " "Add or change a related_name argument to the definition "
"for 'invalid_models_tests.Model.m2m_1' or " "for 'invalid_models_tests.Model.m2m_1' or "
@ -1531,9 +1536,9 @@ class ComplexClashTests(SimpleTestCase):
id='fields.E305', id='fields.E305',
), ),
Error( Error(
"Reverse accessor for 'invalid_models_tests.Model.m2m_2' " "Reverse accessor 'Target.src_safe' for "
"clashes with reverse accessor for " "'invalid_models_tests.Model.m2m_2' clashes with reverse "
"'invalid_models_tests.Model.foreign_2'.", "accessor for 'invalid_models_tests.Model.foreign_2'.",
hint=( hint=(
"Add or change a related_name argument to the definition " "Add or change a related_name argument to the definition "
"for 'invalid_models_tests.Model.m2m_2' or " "for 'invalid_models_tests.Model.m2m_2' or "
@ -1564,16 +1569,16 @@ class ComplexClashTests(SimpleTestCase):
other_parent = models.OneToOneField(Parent, models.CASCADE) other_parent = models.OneToOneField(Parent, models.CASCADE)
errors = [ errors = [
('fields.E304', 'accessor', 'parent_ptr', 'other_parent'), ('fields.E304', 'accessor', " 'Parent.child'", 'parent_ptr', 'other_parent'),
('fields.E305', 'query name', 'parent_ptr', 'other_parent'), ('fields.E305', 'query name', '', 'parent_ptr', 'other_parent'),
('fields.E304', 'accessor', 'other_parent', 'parent_ptr'), ('fields.E304', 'accessor', " 'Parent.child'", 'other_parent', 'parent_ptr'),
('fields.E305', 'query name', 'other_parent', 'parent_ptr'), ('fields.E305', 'query name', '', 'other_parent', 'parent_ptr'),
] ]
self.assertEqual(Child.check(), [ self.assertEqual(Child.check(), [
Error( Error(
"Reverse %s for 'invalid_models_tests.Child.%s' clashes with " "Reverse %s%s for 'invalid_models_tests.Child.%s' clashes with "
"reverse %s for 'invalid_models_tests.Child.%s'." "reverse %s for 'invalid_models_tests.Child.%s'."
% (attr, field_name, attr, clash_name), % (attr, rel_name, field_name, attr, clash_name),
hint=( hint=(
"Add or change a related_name argument to the definition " "Add or change a related_name argument to the definition "
"for 'invalid_models_tests.Child.%s' or " "for 'invalid_models_tests.Child.%s' or "
@ -1582,7 +1587,7 @@ class ComplexClashTests(SimpleTestCase):
obj=Child._meta.get_field(field_name), obj=Child._meta.get_field(field_name),
id=error_id, id=error_id,
) )
for error_id, attr, field_name, clash_name in errors for error_id, attr, rel_name, field_name, clash_name in errors
]) ])

View File

@ -292,8 +292,9 @@ class AbstractInheritanceTests(SimpleTestCase):
Foo._meta.get_field('foo').check(), Foo._meta.get_field('foo').check(),
[ [
Error( Error(
"Reverse accessor for 'model_inheritance.Foo.foo' clashes " "Reverse accessor 'Descendant.foo' for "
"with field name 'model_inheritance.Descendant.foo'.", "'model_inheritance.Foo.foo' clashes with field name "
"'model_inheritance.Descendant.foo'.",
hint=( hint=(
"Rename field 'model_inheritance.Descendant.foo', or " "Rename field 'model_inheritance.Descendant.foo', or "
"add/change a related_name argument to the definition " "add/change a related_name argument to the definition "