From 795697dc225af79958f16610906df3a31a4b2eaa Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Tue, 31 Mar 2009 21:48:29 +0000 Subject: [PATCH] [1.0.X] Fixed #8746: Check data in raw_id_fields more closely. Thanks, dgouldin Backport of r10233 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10294 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/forms/models.py | 3 +++ tests/modeltests/model_forms/models.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/django/forms/models.py b/django/forms/models.py index 57dca31fca..abdb83f6a9 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -740,6 +740,7 @@ class ModelMultipleChoiceField(ModelChoiceField): 'list': _(u'Enter a list of values.'), 'invalid_choice': _(u'Select a valid choice. %s is not one of the' u' available choices.'), + 'invalid_pk_value': _(u'"%s" is not a valid value for a primary key.') } def __init__(self, queryset, cache_choices=False, required=True, @@ -762,6 +763,8 @@ class ModelMultipleChoiceField(ModelChoiceField): obj = self.queryset.get(pk=val) except self.queryset.model.DoesNotExist: raise ValidationError(self.error_messages['invalid_choice'] % val) + except ValueError: + raise ValidationError(self.error_messages['invalid_pk_value'] % val) else: final_values.append(obj) return final_values diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 1e72a553b1..7a42bc226c 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -813,6 +813,10 @@ ValidationError: [u'Select a valid choice. 100 is not one of the available choic Traceback (most recent call last): ... ValidationError: [u'Enter a list of values.'] +>>> f.clean(['fail']) +Traceback (most recent call last): +... +ValidationError: [u'"fail" is not a valid value for a primary key.'] # Add a Category object *after* the ModelMultipleChoiceField has already been # instantiated. This proves clean() checks the database during clean() rather