1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

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

@@ -923,6 +923,21 @@ class ForeignKey(ForeignObject):
}, # 'pk' is included for backwards compatibility
)
def resolve_related_fields(self):
related_fields = super().resolve_related_fields()
for from_field, to_field in related_fields:
if to_field and to_field.model != self.remote_field.model._meta.concrete_model:
raise exceptions.FieldError(
"'%s.%s' refers to field '%s' which is not local to model "
"'%s'." % (
self.model._meta.label,
self.name,
to_field.name,
self.remote_field.model._meta.concrete_model._meta.label,
)
)
return related_fields
def get_attname(self):
return '%s_id' % self.name