mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-25 00:34:21 +03:00
Updated supported values for the NullBooleanField (#5387)
* Updated supported values for the NullBooleanField. * Added check for unhashable types in NullBooleanField.
This commit is contained in:
parent
e42eb42d49
commit
79be20a7c6
|
@ -688,8 +688,22 @@ class NullBooleanField(Field):
|
||||||
'invalid': _('"{input}" is not a valid boolean.')
|
'invalid': _('"{input}" is not a valid boolean.')
|
||||||
}
|
}
|
||||||
initial = None
|
initial = None
|
||||||
TRUE_VALUES = {'t', 'T', 'true', 'True', 'TRUE', '1', 1, True}
|
TRUE_VALUES = {
|
||||||
FALSE_VALUES = {'f', 'F', 'false', 'False', 'FALSE', '0', 0, 0.0, False}
|
't', 'T',
|
||||||
|
'y', 'Y', 'yes', 'YES',
|
||||||
|
'true', 'True', 'TRUE',
|
||||||
|
'on', 'On', 'ON',
|
||||||
|
'1', 1,
|
||||||
|
True
|
||||||
|
}
|
||||||
|
FALSE_VALUES = {
|
||||||
|
'f', 'F',
|
||||||
|
'n', 'N', 'no', 'NO',
|
||||||
|
'false', 'False', 'FALSE',
|
||||||
|
'off', 'Off', 'OFF',
|
||||||
|
'0', 0, 0.0,
|
||||||
|
False
|
||||||
|
}
|
||||||
NULL_VALUES = {'n', 'N', 'null', 'Null', 'NULL', '', None}
|
NULL_VALUES = {'n', 'N', 'null', 'Null', 'NULL', '', None}
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
|
@ -698,12 +712,15 @@ class NullBooleanField(Field):
|
||||||
super(NullBooleanField, self).__init__(**kwargs)
|
super(NullBooleanField, self).__init__(**kwargs)
|
||||||
|
|
||||||
def to_internal_value(self, data):
|
def to_internal_value(self, data):
|
||||||
|
try:
|
||||||
if data in self.TRUE_VALUES:
|
if data in self.TRUE_VALUES:
|
||||||
return True
|
return True
|
||||||
elif data in self.FALSE_VALUES:
|
elif data in self.FALSE_VALUES:
|
||||||
return False
|
return False
|
||||||
elif data in self.NULL_VALUES:
|
elif data in self.NULL_VALUES:
|
||||||
return None
|
return None
|
||||||
|
except TypeError: # Input is an unhashable type
|
||||||
|
pass
|
||||||
self.fail('invalid', input=data)
|
self.fail('invalid', input=data)
|
||||||
|
|
||||||
def to_representation(self, value):
|
def to_representation(self, value):
|
||||||
|
|
|
@ -587,7 +587,7 @@ class TestBooleanField(FieldValues):
|
||||||
[],
|
[],
|
||||||
{},
|
{},
|
||||||
)
|
)
|
||||||
field = serializers.BooleanField()
|
field = self.field
|
||||||
for input_value in inputs:
|
for input_value in inputs:
|
||||||
with pytest.raises(serializers.ValidationError) as exc_info:
|
with pytest.raises(serializers.ValidationError) as exc_info:
|
||||||
field.run_validation(input_value)
|
field.run_validation(input_value)
|
||||||
|
@ -595,7 +595,7 @@ class TestBooleanField(FieldValues):
|
||||||
assert exc_info.value.detail == expected
|
assert exc_info.value.detail == expected
|
||||||
|
|
||||||
|
|
||||||
class TestNullBooleanField(FieldValues):
|
class TestNullBooleanField(TestBooleanField):
|
||||||
"""
|
"""
|
||||||
Valid and invalid values for `BooleanField`.
|
Valid and invalid values for `BooleanField`.
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user