Loading django/forms/models.py +3 −0 Original line number Diff line number Diff line Loading @@ -760,6 +760,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, Loading @@ -782,6 +783,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 tests/modeltests/model_forms/models.py +4 −0 Original line number Diff line number Diff line Loading @@ -857,6 +857,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 Loading Loading
django/forms/models.py +3 −0 Original line number Diff line number Diff line Loading @@ -760,6 +760,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, Loading @@ -782,6 +783,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
tests/modeltests/model_forms/models.py +4 −0 Original line number Diff line number Diff line Loading @@ -857,6 +857,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 Loading