[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
This commit is contained in:
Jacob Kaplan-Moss 2009-03-31 21:48:29 +00:00
parent 67a5880162
commit 795697dc22
2 changed files with 7 additions and 0 deletions

View File

@ -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

View File

@ -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