#11303: changed_data wrong work with BooleanField
-------------------------------+--------------------------------------------
Reporter: oduvan | Owner: nobody
Status: new | Milestone:
Component: Forms | Version: 1.1-beta-1
Resolution: | Keywords:
Stage: Accepted | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
-------------------------------+--------------------------------------------
Comment (by margieroginski):
I've just encountered this problem again, but in a slightly different way.
I have a !BooleanField in my model and it is has value False. When I
create the corresponding !BooleanField in my modelForm, the initial value
sent out in the hidden input is '0'. In other words, request.POST
contains
{{{
u'initial-requireResultOnClose': [u'0']
}}}
If I then check the checkbox, the field comes back with 'on', like this:
{{{
u'requireResultOnClose': [u'on']
}}}
The _has_changed() method for !CheckBoxInput() returns True when it
compares u'0' and u'on' due to the fact that bool(u'0') is True, but it
should really return False.
I have changed _has_changed() for !CheckBoxInput() to look like this,
which seems to work form my cases:
def _has_changed(self, initial, data):
# Sometimes data or initial could be None or u'' which should be
the
# same thing as False.
if initial in ('False', '0'):
initial = False
return bool(initial) != bool(data)
In fields.py, !BooleanField's clean() method does something similar, so
I'm hoping this is the right direction. In general it seems very
difficult to compare all these values such as False, 'False', u'0', u'1',
True, 'True' and get it right. The hidden initial stuff adds an extra
complication due to the fact that the hidden input is rendered through the
hidden_widget() widget, which uses different values than those used by the
!CheckBoxInput. IE, in the !CheckBoxInput widget when data is posted, the
value 'on' indicates it is checked and no input at all in the post data
indicates it is unchecked. But when using the !CheckBoxInput's
hidden_widget() widget, you see values u'0' and u'1', and it seems that
these are really not taken into account in the current code base, probably
due to the fact that few people are using the show_hidden_initial
functionality.
--
Ticket URL: <http://code.djangoproject.com/ticket/11303#comment:9>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.