Fixed 31207 -- Prevented references to non-local remote fields in ForeignKey.to_field.

Thanks Simon Charette for the initial patch and review.
This commit is contained in:
Hasan Ramezani 2020-01-29 14:29:09 +01:00 committed by Mariusz Felisiak
parent 4e8d89020c
commit a97111eabf
3 changed files with 37 additions and 0 deletions

View file

@ -2,6 +2,7 @@ from decimal import Decimal
from django.apps import apps
from django.core import checks
from django.core.exceptions import FieldError
from django.db import models
from django.test import TestCase, skipIfDBFeature
from django.test.utils import isolate_apps
@ -128,3 +129,21 @@ class ForeignKeyTests(TestCase):
with self.assertRaisesMessage(ValueError, 'Cannot resolve output_field'):
Foo._meta.get_field('bar').get_col('alias')
@isolate_apps('model_fields')
def test_non_local_to_field(self):
class Parent(models.Model):
key = models.IntegerField(unique=True)
class Child(Parent):
pass
class Related(models.Model):
child = models.ForeignKey(Child, on_delete=models.CASCADE, to_field='key')
msg = (
"'model_fields.Related.child' refers to field 'key' which is not "
"local to model 'model_fields.Child'."
)
with self.assertRaisesMessage(FieldError, msg):
Related._meta.get_field('child').related_fields