summaryrefslogtreecommitdiff
path: root/django/newforms/fields.py
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2007-01-24 05:23:19 +0000
committerAdrian Holovaty <adrian@holovaty.com>2007-01-24 05:23:19 +0000
commit788f8f74540b87f79fbfbe44a4b2f00502d66d8a (patch)
treed21e6c5a6fc94d113311c08781fb036f48977ec3 /django/newforms/fields.py
parent85fe1079968209e380141c67fe7ef5048d28584b (diff)
newforms: Implemented NullBooleanField and NullBooleanSelect
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4411 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/newforms/fields.py')
-rw-r--r--django/newforms/fields.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/django/newforms/fields.py b/django/newforms/fields.py
index e96cb4a2d1..bcd30dbc92 100644
--- a/django/newforms/fields.py
+++ b/django/newforms/fields.py
@@ -4,7 +4,7 @@ Field classes
from django.utils.translation import gettext
from util import ErrorList, ValidationError, smart_unicode
-from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, CheckboxInput, Select, SelectMultiple
+from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple
import datetime
import re
import time
@@ -15,7 +15,7 @@ __all__ = (
'DEFAULT_TIME_INPUT_FORMATS', 'TimeField',
'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField',
'RegexField', 'EmailField', 'URLField', 'BooleanField',
- 'ChoiceField', 'MultipleChoiceField',
+ 'ChoiceField', 'NullBooleanField', 'MultipleChoiceField',
'ComboField', 'MultiValueField',
'SplitDateTimeField',
)
@@ -317,6 +317,16 @@ class BooleanField(Field):
super(BooleanField, self).clean(value)
return bool(value)
+class NullBooleanField(BooleanField):
+ """
+ A field whose valid values are None, True and False. Invalid values are
+ cleaned to None.
+ """
+ widget = NullBooleanSelect
+
+ def clean(self, value):
+ return {True: True, False: False}.get(value, None)
+
class ChoiceField(Field):
def __init__(self, choices=(), required=True, widget=Select, label=None, initial=None):
if isinstance(widget, type):