diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-04-29 14:25:06 +0200 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-05-08 23:20:05 +0200 |
| commit | a3c8201b77002645f86c39f978fe132cb2dbab14 (patch) | |
| tree | bdb7d9a6252df1586265d89c0fb7c74d9703072a | |
| parent | 521fe472e5dcc48274cc511f8fdf0f1c912402c1 (diff) | |
[1.4.x] Fixed #17976 -- Made forms.BooleanField pickleable.
Backport of 9350d1d59c1a4e6a9ac246a808f55da35de0df69 from master.
This was a regression in Django 1.4.
Thanks bronger for the report and claudep for the patch.
| -rw-r--r-- | django/forms/widgets.py | 11 | ||||
| -rw-r--r-- | tests/regressiontests/forms/tests/fields.py | 4 |
2 files changed, 11 insertions, 4 deletions
diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 1fbef98deb..4813af69e9 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -487,15 +487,18 @@ class TimeInput(Input): pass 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): def __init__(self, attrs=None, check_test=None): super(CheckboxInput, self).__init__(attrs) # check_test is a callable that takes a value and returns True # if the checkbox should be checked for that value. - if check_test is None: - self.check_test = lambda v: not (v is False or v is None or v == '') - else: - self.check_test = check_test + self.check_test = boolean_check if check_test is None else check_test def render(self, name, value, attrs=None): final_attrs = self.build_attrs(attrs, type='checkbox', name=name) diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py index 03e0ff65a3..fe98c925e4 100644 --- a/tests/regressiontests/forms/tests/fields.py +++ b/tests/regressiontests/forms/tests/fields.py @@ -25,6 +25,7 @@ Other than that, the Field subclasses have class-specific options for __init__(). For example, CharField has a max_length option. """ import datetime +import pickle import re import os import urllib2 @@ -762,6 +763,9 @@ class FieldsTests(SimpleTestCase): 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 ################################################################# def test_choicefield_1(self): |
