Fixed #35580 -- Allowed related fields referencing auto-created through models.

This commit is contained in:
jason_bruce 2024-07-05 14:41:01 -04:00 committed by Sarah Boyce
parent 759abc4daf
commit af84bcc8d1
2 changed files with 20 additions and 1 deletions

View File

@ -187,7 +187,9 @@ class RelatedField(FieldCacheMixin, Field):
return errors
def _check_relation_model_exists(self):
rel_is_missing = self.remote_field.model not in self.opts.apps.get_models()
rel_is_missing = self.remote_field.model not in self.opts.apps.get_models(
include_auto_created=True
)
rel_is_string = isinstance(self.remote_field.model, str)
model_name = (
self.remote_field.model

View File

@ -89,6 +89,23 @@ class RelativeFieldTests(SimpleTestCase):
field = Model._meta.get_field("m2m")
self.assertEqual(field.check(from_model=Model), [])
@isolate_apps("invalid_models_tests")
def test_auto_created_through_model(self):
class OtherModel(models.Model):
pass
class M2MModel(models.Model):
many_to_many_rel = models.ManyToManyField(OtherModel)
class O2OModel(models.Model):
one_to_one_rel = models.OneToOneField(
"invalid_models_tests.M2MModel_many_to_many_rel",
on_delete=models.CASCADE,
)
field = O2OModel._meta.get_field("one_to_one_rel")
self.assertEqual(field.check(from_model=O2OModel), [])
def test_many_to_many_with_useless_options(self):
class Model(models.Model):
name = models.CharField(max_length=20)