Ticket #11860: nullbooleanselect_patch.diff

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

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

  • django/forms/widgets.py

     
    452452                False: False}.get(value, None)
    453453
    454454    def _has_changed(self, initial, data):
    455         # Sometimes data or initial could be None or u'' which should be the
    456         # same thing as False.
    457         return bool(initial) != bool(data)
     455        # For a NullBooleanSelect, None (unknown) and False (No)
     456        # are not the same
     457        if initial is not None:
     458            initial = bool(initial)
     459        if data is not None:
     460            data = bool(data)
     461        return initial != data
    458462
    459463class SelectMultiple(Select):
    460464    def render(self, name, value, attrs=None, choices=()):
  • tests/regressiontests/forms/widgets.py

     
    532532<option value="2">Yes</option>
    533533<option value="3" selected="selected">No</option>
    534534</select>
     535>>> w._has_changed(False, None)
     536True
     537>>> w._has_changed(None, False)
     538True
     539>>> w._has_changed(None, None)
     540False
     541>>> w._has_changed(False, False)
     542False
     543>>> w._has_changed(True, False)
     544True
     545>>> w._has_changed(True, None)
     546True
     547>>> w._has_changed(True, True)
     548False
    535549
    536550""" + \
    537551r""" # [This concatenation is to keep the string below the jython's 32K limit].
Back to Top