Fixed #17976 -- Made forms.BooleanField pickleable.

This was a regression in Django 1.4.
Thanks bronger for the report and claudep for the patch.
This commit is contained in:
Aymeric Augustin 2012-04-29 14:25:06 +02:00
parent 02a5b41db4
commit 9350d1d59c
2 changed files with 11 additions and 4 deletions

View File

@ -487,15 +487,18 @@ class TimeInput(Input):
pass pass
return super(TimeInput, self)._has_changed(self._format_value(initial), data) return super(TimeInput, self)._has_changed(self._format_value(initial), data)
# Defined at module level so that CheckboxInput is picklable (#17976)
def boolean_check(v):
return not (v is False or v is None or v == '')
class CheckboxInput(Widget): class CheckboxInput(Widget):
def __init__(self, attrs=None, check_test=None): def __init__(self, attrs=None, check_test=None):
super(CheckboxInput, self).__init__(attrs) super(CheckboxInput, self).__init__(attrs)
# check_test is a callable that takes a value and returns True # check_test is a callable that takes a value and returns True
# if the checkbox should be checked for that value. # if the checkbox should be checked for that value.
if check_test is None: self.check_test = boolean_check if check_test is None else check_test
self.check_test = lambda v: not (v is False or v is None or v == '')
else:
self.check_test = check_test
def render(self, name, value, attrs=None): def render(self, name, value, attrs=None):
final_attrs = self.build_attrs(attrs, type='checkbox', name=name) final_attrs = self.build_attrs(attrs, type='checkbox', name=name)

View File

@ -25,6 +25,7 @@ Other than that, the Field subclasses have class-specific options for
__init__(). For example, CharField has a max_length option. __init__(). For example, CharField has a max_length option.
""" """
import datetime import datetime
import pickle
import re import re
import os import os
import warnings import warnings
@ -690,6 +691,9 @@ class FieldsTests(SimpleTestCase):
self.assertEqual(False, f.clean('false')) self.assertEqual(False, f.clean('false'))
self.assertEqual(False, f.clean('FaLsE')) self.assertEqual(False, f.clean('FaLsE'))
def test_boolean_picklable(self):
self.assertIsInstance(pickle.loads(pickle.dumps(BooleanField())), BooleanField)
# ChoiceField ################################################################# # ChoiceField #################################################################
def test_choicefield_1(self): def test_choicefield_1(self):