Code

Ticket #11860: nullbooleanselect_patch.diff

File nullbooleanselect_patch.diff, 1.5 KB (added by matiasb, 4 years ago)

Fixed _has_changed method of NullBooleanSelect widget to distinguish between None and False (updated)

Line 
1Index: django/forms/widgets.py
2===================================================================
3--- django/forms/widgets.py     (revision 12488)
4+++ django/forms/widgets.py     (working copy)
5@@ -452,9 +452,13 @@
6                 False: False}.get(value, None)
7 
8     def _has_changed(self, initial, data):
9-        # Sometimes data or initial could be None or u'' which should be the
10-        # same thing as False.
11-        return bool(initial) != bool(data)
12+        # For a NullBooleanSelect, None (unknown) and False (No)
13+        # are not the same
14+        if initial is not None:
15+            initial = bool(initial)
16+        if data is not None:
17+            data = bool(data)
18+        return initial != data
19 
20 class SelectMultiple(Select):
21     def render(self, name, value, attrs=None, choices=()):
22Index: tests/regressiontests/forms/widgets.py
23===================================================================
24--- tests/regressiontests/forms/widgets.py      (revision 12488)
25+++ tests/regressiontests/forms/widgets.py      (working copy)
26@@ -532,6 +532,20 @@
27 <option value="2">Yes</option>
28 <option value="3" selected="selected">No</option>
29 </select>
30+>>> w._has_changed(False, None)
31+True
32+>>> w._has_changed(None, False)
33+True
34+>>> w._has_changed(None, None)
35+False
36+>>> w._has_changed(False, False)
37+False
38+>>> w._has_changed(True, False)
39+True
40+>>> w._has_changed(True, None)
41+True
42+>>> w._has_changed(True, True)
43+False
44 
45 """ + \
46 r""" # [This concatenation is to keep the string below the jython's 32K limit].