From a97111eabf39e50625d7a8acc6cd46f4c5dad0da Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Wed, 29 Jan 2020 14:29:09 +0100 Subject: Fixed 31207 -- Prevented references to non-local remote fields in ForeignKey.to_field. Thanks Simon Charette for the initial patch and review. --- tests/model_fields/test_foreignkey.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'tests/model_fields') diff --git a/tests/model_fields/test_foreignkey.py b/tests/model_fields/test_foreignkey.py index 51e76c4052..d30cca9b5c 100644 --- a/tests/model_fields/test_foreignkey.py +++ b/tests/model_fields/test_foreignkey.py @@ -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 -- cgit v1.3