Allow nullable BooleanField in Django 2.1

This commit is contained in:
Ryan P Kilby 2018-09-12 17:43:51 -07:00
parent 18d9dc3657
commit 75d6ad3e20

View File

@ -10,6 +10,7 @@ import re
import uuid import uuid
from collections import OrderedDict from collections import OrderedDict
import django
from django.conf import settings from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ValidationError as DjangoValidationError from django.core.exceptions import ValidationError as DjangoValidationError
@ -674,9 +675,11 @@ class BooleanField(Field):
'0', 0, 0.0, '0', 0, 0.0,
False False
} }
NULL_VALUES = {'n', 'N', 'null', 'Null', 'NULL', '', None}
def __init__(self, **kwargs): def __init__(self, **kwargs):
assert 'allow_null' not in kwargs, '`allow_null` is not a valid option. Use `NullBooleanField` instead.' if django.VERSION < (2, 1):
assert 'allow_null' not in kwargs, '`allow_null` is not a valid option. Use `NullBooleanField` instead.'
super(BooleanField, self).__init__(**kwargs) super(BooleanField, self).__init__(**kwargs)
def to_internal_value(self, data): def to_internal_value(self, data):
@ -685,6 +688,8 @@ class BooleanField(Field):
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 and self.allow_null:
return None
except TypeError: # Input is an unhashable type except TypeError: # Input is an unhashable type
pass pass
self.fail('invalid', input=data) self.fail('invalid', input=data)
@ -694,6 +699,8 @@ class BooleanField(Field):
return True return True
elif value in self.FALSE_VALUES: elif value in self.FALSE_VALUES:
return False return False
if value in self.NULL_VALUES and self.allow_null:
return None
return bool(value) return bool(value)