Created
May 19, 2011 12:01
-
-
Save Gautier/980597 to your computer and use it in GitHub Desktop.
Yes No boolean field for
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
class YesNoNullBooleanSelect(forms.Select): | |
""" | |
A Select Widget intended to be used with YesNoBooleanField. | |
""" | |
def value_from_datadict(self, data, files, name): | |
value = data.get(name, None) | |
return {u'2': True, | |
True: True, | |
'True': True, | |
'yes': True, | |
u'3': False, | |
'False': False, | |
'no': False, | |
False: False}.get(value, None) | |
class YesNoBooleanField(forms.NullBooleanField): | |
""" | |
A field whose valid values are None, True and False. Invalid values are | |
cleaned to None. | |
""" | |
widget = YesNoNullBooleanSelect | |
def to_python(self, value): | |
if value in (True, 'True', '1', 'yes'): | |
return True | |
elif value in (False, 'False', '0', 'no'): | |
return False | |
else: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment