diff options
| author | Claude Paroz <claude@2xlibre.net> | 2013-11-18 18:24:56 +0100 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2013-11-18 18:24:56 +0100 |
| commit | a0f3eeccf3d5a90f9914bfe20b15df05673ea59d (patch) | |
| tree | da91c208646cdef2a8f1dd535ab5bcd07d566a52 /django/forms | |
| parent | 4a00f132e0cc5246f7e7bd04b6d84a9d9ea4a0c1 (diff) | |
Fixed #21397 -- Re-added flexibility to TypedChoiceField coercion
Thanks Elec for the report and Simon Charette for the review.
Diffstat (limited to 'django/forms')
| -rw-r--r-- | django/forms/fields.py | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py index c95930a2d0..56d0e316f2 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -822,12 +822,10 @@ class TypedChoiceField(ChoiceField): self.empty_value = kwargs.pop('empty_value', '') super(TypedChoiceField, self).__init__(*args, **kwargs) - def to_python(self, value): + def _coerce(self, value): """ - Validates that the value is in self.choices and can be coerced to the - right type. + Validate that the value can be coerced to the right type (if not empty). """ - value = super(TypedChoiceField, self).to_python(value) if value == self.empty_value or value in self.empty_values: return self.empty_value try: @@ -840,6 +838,10 @@ class TypedChoiceField(ChoiceField): ) return value + def clean(self, value): + value = super(TypedChoiceField, self).clean(value) + return self._coerce(value) + class MultipleChoiceField(ChoiceField): hidden_widget = MultipleHiddenInput @@ -889,12 +891,11 @@ class TypedMultipleChoiceField(MultipleChoiceField): self.empty_value = kwargs.pop('empty_value', []) super(TypedMultipleChoiceField, self).__init__(*args, **kwargs) - def to_python(self, value): + def _coerce(self, value): """ Validates that the values are in self.choices and can be coerced to the right type. """ - value = super(TypedMultipleChoiceField, self).to_python(value) if value == self.empty_value or value in self.empty_values: return self.empty_value new_value = [] @@ -909,6 +910,10 @@ class TypedMultipleChoiceField(MultipleChoiceField): ) return new_value + def clean(self, value): + value = super(TypedMultipleChoiceField, self).clean(value) + return self._coerce(value) + def validate(self, value): if value != self.empty_value: super(TypedMultipleChoiceField, self).validate(value) |
