diff options
| author | Tom Christie | 2014-09-23 14:30:17 +0100 |
|---|---|---|
| committer | Tom Christie | 2014-09-23 14:30:17 +0100 |
| commit | 0404f09a7e69f533038d47ca25caad90c0c2659f (patch) | |
| tree | eae2ceb609d868d02ba84c09d457b3ae6691f33b /rest_framework/fields.py | |
| parent | f22d0afc3dfc7478e084d1d6ed6b53f71641dec6 (diff) | |
| download | django-rest-framework-0404f09a7e69f533038d47ca25caad90c0c2659f.tar.bz2 | |
NullBooleanField
Diffstat (limited to 'rest_framework/fields.py')
| -rw-r--r-- | rest_framework/fields.py | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py index f5bae734..f859658a 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -289,6 +289,10 @@ class BooleanField(Field): TRUE_VALUES = set(('t', 'T', 'true', 'True', 'TRUE', '1', 1, True)) FALSE_VALUES = set(('f', 'F', 'false', 'False', 'FALSE', '0', 0, 0.0, False)) + def __init__(self, **kwargs): + assert 'allow_null' not in kwargs, '`allow_null` is not a valid option. Use `NullBooleanField` instead.' + super(BooleanField, self).__init__(**kwargs) + def to_internal_value(self, data): if data in self.TRUE_VALUES: return True @@ -297,7 +301,38 @@ class BooleanField(Field): self.fail('invalid', input=data) def to_representation(self, value): - if value is None: + if value in self.TRUE_VALUES: + return True + elif value in self.FALSE_VALUES: + return False + return bool(value) + + +class NullBooleanField(Field): + default_error_messages = { + 'invalid': _('`{input}` is not a valid boolean.') + } + default_empty_html = None + TRUE_VALUES = set(('t', 'T', 'true', 'True', 'TRUE', '1', 1, True)) + FALSE_VALUES = set(('f', 'F', 'false', 'False', 'FALSE', '0', 0, 0.0, False)) + NULL_VALUES = set(('n', 'N', 'null', 'Null', 'NULL', '', None)) + + def __init__(self, **kwargs): + assert 'allow_null' not in kwargs, '`allow_null` is not a valid option.' + kwargs['allow_null'] = True + super(NullBooleanField, self).__init__(**kwargs) + + def to_internal_value(self, data): + if data in self.TRUE_VALUES: + return True + elif data in self.FALSE_VALUES: + return False + elif data in self.NULL_VALUES: + return None + self.fail('invalid', input=data) + + def to_representation(self, value): + if value in self.NULL_VALUES: return None if value in self.TRUE_VALUES: return True |
