mirror of https://github.com/django/django.git
Refs #29227 -- Simplified NullBooleanField by making it subclass BooleanField.
This commit is contained in:
parent
6421bd702f
commit
281c0223b3
|
@ -251,10 +251,7 @@ class BooleanFieldListFilter(FieldListFilter):
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
FieldListFilter.register(
|
FieldListFilter.register(lambda f: isinstance(f, models.BooleanField), BooleanFieldListFilter)
|
||||||
lambda f: isinstance(f, (models.BooleanField, models.NullBooleanField)),
|
|
||||||
BooleanFieldListFilter
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class ChoicesFieldListFilter(FieldListFilter):
|
class ChoicesFieldListFilter(FieldListFilter):
|
||||||
|
|
|
@ -396,7 +396,7 @@ def display_for_field(value, field, empty_value_display):
|
||||||
return dict(field.flatchoices).get(value, empty_value_display)
|
return dict(field.flatchoices).get(value, empty_value_display)
|
||||||
# BooleanField needs special-case null-handling, so it comes before the
|
# BooleanField needs special-case null-handling, so it comes before the
|
||||||
# general null test.
|
# general null test.
|
||||||
elif isinstance(field, (models.BooleanField, models.NullBooleanField)):
|
elif isinstance(field, models.BooleanField):
|
||||||
return _boolean_icon(value)
|
return _boolean_icon(value)
|
||||||
elif value is None:
|
elif value is None:
|
||||||
return empty_value_display
|
return empty_value_display
|
||||||
|
|
|
@ -1956,10 +1956,10 @@ class GenericIPAddressField(Field):
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
class NullBooleanField(Field):
|
class NullBooleanField(BooleanField):
|
||||||
empty_strings_allowed = False
|
|
||||||
default_error_messages = {
|
default_error_messages = {
|
||||||
'invalid': _("'%(value)s' value must be either None, True or False."),
|
'invalid': _("'%(value)s' value must be either None, True or False."),
|
||||||
|
'invalid_nullable': _("'%(value)s' value must be either None, True or False."),
|
||||||
}
|
}
|
||||||
description = _("Boolean (Either True, False or None)")
|
description = _("Boolean (Either True, False or None)")
|
||||||
|
|
||||||
|
@ -1977,35 +1977,6 @@ class NullBooleanField(Field):
|
||||||
def get_internal_type(self):
|
def get_internal_type(self):
|
||||||
return "NullBooleanField"
|
return "NullBooleanField"
|
||||||
|
|
||||||
def to_python(self, value):
|
|
||||||
if value is None:
|
|
||||||
return None
|
|
||||||
if value in (True, False):
|
|
||||||
return bool(value)
|
|
||||||
if value in ('None',):
|
|
||||||
return None
|
|
||||||
if value in ('t', 'True', '1'):
|
|
||||||
return True
|
|
||||||
if value in ('f', 'False', '0'):
|
|
||||||
return False
|
|
||||||
raise exceptions.ValidationError(
|
|
||||||
self.error_messages['invalid'],
|
|
||||||
code='invalid',
|
|
||||||
params={'value': value},
|
|
||||||
)
|
|
||||||
|
|
||||||
def get_prep_value(self, value):
|
|
||||||
value = super().get_prep_value(value)
|
|
||||||
if value is None:
|
|
||||||
return None
|
|
||||||
return self.to_python(value)
|
|
||||||
|
|
||||||
def formfield(self, **kwargs):
|
|
||||||
return super().formfield(**{
|
|
||||||
'form_class': forms.NullBooleanField,
|
|
||||||
**kwargs,
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
class PositiveIntegerRelDbTypeMixin:
|
class PositiveIntegerRelDbTypeMixin:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue