From 10e83d48a3b199f9c8c73882be1ded5016f86a1a Mon Sep 17 00:00:00 2001 From: Anubhav Joshi Date: Wed, 2 Jul 2014 10:13:07 +0530 Subject: [PATCH] Fixed #22935 -- Changed ForeignKey.default_error_messages['invalid'] to refer to correct field. Thanks Tim Graham for suggestion and review. --- django/db/models/fields/related.py | 7 +++++-- docs/releases/1.8.txt | 7 +++++++ tests/validation/models.py | 1 + tests/validation/tests.py | 7 ++++++- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index cd592d587c..e277e1025d 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -1600,7 +1600,7 @@ class ForeignObject(RelatedField): class ForeignKey(ForeignObject): empty_strings_allowed = False default_error_messages = { - 'invalid': _('%(model)s instance with pk %(pk)r does not exist.') + 'invalid': _('%(model)s instance with %(field)s %(value)r does not exist.') } description = _("Foreign Key (type determined by related field)") @@ -1707,7 +1707,10 @@ class ForeignKey(ForeignObject): raise exceptions.ValidationError( self.error_messages['invalid'], code='invalid', - params={'model': self.rel.to._meta.verbose_name, 'pk': value}, + params={ + 'model': self.rel.to._meta.verbose_name, 'pk': value, + 'field': self.rel.field_name, 'value': value, + }, # 'pk' is included for backwards compatibilty ) def get_attname(self): diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index 381700557e..8a736aeb45 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -391,6 +391,13 @@ Miscellaneous * Support for SpatiaLite < 2.4 has been dropped. +* ``ForeignKey.default_error_message['invalid']`` has been changed from + ``'%(model)s instance with pk %(pk)r does not exist.'`` to + ``'%(model)s instance with %(field)s %(value)r does not exist.'`` If you are + using this message in your own code, please update the list of interpolated + parameters. Internally, Django will continue to provide the + ``pk`` parameter in ``params`` for backwards compatibility. + .. _deprecated-features-1.8: Features deprecated in 1.8 diff --git a/tests/validation/models.py b/tests/validation/models.py index 157b8cb158..1fe4aeb104 100644 --- a/tests/validation/models.py +++ b/tests/validation/models.py @@ -18,6 +18,7 @@ class ModelToValidate(models.Model): number = models.IntegerField(db_column='number_val') parent = models.ForeignKey('self', blank=True, null=True, limit_choices_to={'number': 10}) email = models.EmailField(blank=True) + ufm = models.ForeignKey('UniqueFieldsModel', to_field='unique_charfield', blank=True, null=True) url = models.URLField(blank=True) f_with_custom_validator = models.IntegerField(blank=True, null=True, validators=[validate_answer_to_universe]) slug = models.SlugField(blank=True) diff --git a/tests/validation/tests.py b/tests/validation/tests.py index 6122444e3d..e1cfd64937 100644 --- a/tests/validation/tests.py +++ b/tests/validation/tests.py @@ -25,7 +25,12 @@ class BaseModelValidationTests(ValidationTestCase): def test_wrong_FK_value_raises_error(self): mtv = ModelToValidate(number=10, name='Some Name', parent_id=3) - self.assertFailsValidation(mtv.full_clean, ['parent']) + self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'parent', + ['model to validate instance with id %r does not exist.' % mtv.parent_id]) + + mtv = ModelToValidate(number=10, name='Some Name', ufm_id='Some Name') + self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'ufm', + ["unique fields model instance with unique_charfield %r does not exist." % mtv.name]) def test_correct_FK_value_validates(self): parent = ModelToValidate.objects.create(number=10, name='Some Name')