1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +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:
saJaeHyukc
2025-03-12 17:39:34 +09:00
committed by Sarah Boyce
parent a0f50c2a48
commit c1a4fccf53
2 changed files with 48 additions and 1 deletions

View File

@@ -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",
),
],
)