1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #26718 -- Added system check for existence of the fields specified by ForeignKey.to_field.

This commit is contained in:
Sergey Fedoseev
2016-06-07 13:55:27 +05:00
committed by Tim Graham
parent f6681393d3
commit 21130ce1a9
3 changed files with 75 additions and 0 deletions

View File

@@ -463,9 +463,32 @@ class ForeignObject(RelatedField):
def check(self, **kwargs):
errors = super(ForeignObject, self).check(**kwargs)
errors.extend(self._check_to_fields_exist())
errors.extend(self._check_unique_target())
return errors
def _check_to_fields_exist(self):
# Skip nonexistent models.
if isinstance(self.remote_field.model, six.string_types):
return []
errors = []
for to_field in self.to_fields:
if to_field:
try:
self.remote_field.model._meta.get_field(to_field)
except exceptions.FieldDoesNotExist:
errors.append(
checks.Error(
"The to_field '%s' doesn't exist on the related "
"model '%s'."
% (to_field, self.remote_field.model._meta.label),
obj=self,
id='fields.E312',
)
)
return errors
def _check_unique_target(self):
rel_is_string = isinstance(self.remote_field.model, six.string_types)
if rel_is_string or not self.requires_unique_target: