mirror of
https://github.com/django/django.git
synced 2025-05-07 15:36:29 +00:00
Fixed #36239 -- Fixed a crash in ManyToManyField.through_fields check when to model is invalid.
Signed-off-by: saJaeHyukc <wogur981208@gmail.com>
This commit is contained in:
parent
a0f50c2a48
commit
c1a4fccf53
@ -1707,13 +1707,18 @@ class ManyToManyField(RelatedField):
|
|||||||
and getattr(field.remote_field, "model", None)
|
and getattr(field.remote_field, "model", None)
|
||||||
== related_model
|
== related_model
|
||||||
):
|
):
|
||||||
|
related_object_name = (
|
||||||
|
related_model
|
||||||
|
if isinstance(related_model, str)
|
||||||
|
else related_model._meta.object_name
|
||||||
|
)
|
||||||
errors.append(
|
errors.append(
|
||||||
checks.Error(
|
checks.Error(
|
||||||
"'%s.%s' is not a foreign key to '%s'."
|
"'%s.%s' is not a foreign key to '%s'."
|
||||||
% (
|
% (
|
||||||
through._meta.object_name,
|
through._meta.object_name,
|
||||||
field_name,
|
field_name,
|
||||||
related_model._meta.object_name,
|
related_object_name,
|
||||||
),
|
),
|
||||||
hint=hint,
|
hint=hint,
|
||||||
obj=self,
|
obj=self,
|
||||||
|
@ -2185,3 +2185,45 @@ class M2mThroughFieldsTests(SimpleTestCase):
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_invalid_to_argument_with_through(self):
|
||||||
|
class Foo(models.Model):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Bar(models.Model):
|
||||||
|
foos = models.ManyToManyField(
|
||||||
|
to="Fo",
|
||||||
|
through="FooBar",
|
||||||
|
through_fields=("bar", "foo"),
|
||||||
|
)
|
||||||
|
|
||||||
|
class FooBar(models.Model):
|
||||||
|
foo = models.ForeignKey("Foo", on_delete=models.CASCADE)
|
||||||
|
bar = models.ForeignKey("Bar", on_delete=models.CASCADE)
|
||||||
|
|
||||||
|
field = Bar._meta.get_field("foos")
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
field.check(from_model=Bar),
|
||||||
|
[
|
||||||
|
Error(
|
||||||
|
"Field defines a relation with model 'Fo', "
|
||||||
|
"which is either not installed, or is abstract.",
|
||||||
|
obj=field,
|
||||||
|
id="fields.E300",
|
||||||
|
),
|
||||||
|
Error(
|
||||||
|
"The model is used as an intermediate model by "
|
||||||
|
"'invalid_models_tests.Bar.foos', "
|
||||||
|
"but it does not have a foreign key to 'Bar' "
|
||||||
|
"or 'invalid_models_tests.Fo'.",
|
||||||
|
obj=FooBar,
|
||||||
|
id="fields.E336",
|
||||||
|
),
|
||||||
|
Error(
|
||||||
|
"'FooBar.foo' is not a foreign key to 'Fo'.",
|
||||||
|
obj=field,
|
||||||
|
id="fields.E339",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user