I have tried to recreate your problem, but I am just not seeing it. You might however find my results working with RadioField
interesting and I can at least point you towards a working solution.
This is your problem line:
form.publishInCollegeCatalog.data = True# NOT WORKING
The simple fix here is to simply do something like this:
form.publishInCollegeCatalog.data = str(True)# WORKING
You are conflating True
with 'True'
:
Working Example:
from collections import namedtuple
from wtforms.validators import Required
from wtforms import Form
from wtforms import RadioField
from webob.multidict import MultiDict
class SimpleForm(Form):
example = RadioField('Label',
choices=[(True,'Truthy'),(False,'Falsey')])
# when this data is processed True is coerced to its
# string representation 'True'
data = {'example': True}
form = SimpleForm(data=MultiDict(data))
# checking form.data here yields - {'example': u'True'}
# This prints the radio markup using the value `True`
print form.example
form.example.data = True
# This prints the radio using the value True
print form.example
What do they Render?
First print:
Second Print:
Explanation:
The RadioField
renders a string representation of the value
part of the choice. This is right in line with the HTML specification. Treated here.
value = string
Gives the default value of the input element.
The value
part of the spec is treated as a string to support a wide range of values. WTForms has no choice but to treat this type as a string when it does its processing step. Its the least common denominator.
But what about my ints?
Once again this is not a problem that I am able to recreate. Every single IntegerField
I create behaves exactly the same way. If you follow the example below your results should be the same.
class SimpleForm(Form):
my_int = IntegerField()
data = {'my_int': int(5)}
form = SimpleForm(data=MultiDict(data))
print form.my_int
form.my_int.data = int(6)
print form.my_int
printing out
<input id="my_int" name="my_int" type="text" value="5">
<input id="my_int" name="my_int" type="text" value="6">
which is what one would expect.