summaryrefslogtreecommitdiff
path: root/tests/model_fields
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2020-01-29 14:29:09 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-01-31 10:19:12 +0100
commita97111eabf39e50625d7a8acc6cd46f4c5dad0da (patch)
treeef47764042db706a115c89aa079c30e54ea897e8 /tests/model_fields
parent4e8d89020cca87fcf484eb6b6b514b4c9bfa592f (diff)
downloaddjango-a97111eabf39e50625d7a8acc6cd46f4c5dad0da.tar.gz
Fixed 31207 -- Prevented references to non-local remote fields in ForeignKey.to_field.
Thanks Simon Charette for the initial patch and review.
Diffstat (limited to 'tests/model_fields')
-rw-r--r--tests/model_fields/test_foreignkey.py19
1 files changed, 19 insertions, 0 deletions
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