mirror of
https://github.com/django/django.git
synced 2025-10-25 06:36:07 +00:00
Fixed #21397 -- Re-added flexibility to TypedChoiceField coercion
Thanks Elec for the report and Simon Charette for the review.
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user