I have a form with a column of checkboxes corresponding to the columns in my database. I'm setting the value of each checkbox (in javascript) to the name of the column, but when I try to read the checkbox value in Flask/Python all I can get is True or False. How do I read the text value of the value attribute of the checkboxes?
Just to complicate things, I'm generating the form as a FieldList of FormFields, so I can't simply hardcode the field names. (Well, I could, but that would make it fragile to schema changes.)
My form code is
class ImportFilterSubForm(Form):
use = BooleanField(
'Use',
render_kw={'class': 'use'}
)
sel = SelectField(
'Maps to:',
choices=[],
render_kw={'class': 'sel'},
validators=[Optional()]
)
class ImportFilterForm(FlaskForm):
rows = FieldList(FormField(ImportFilterSubForm))
My view code, with error handling removed, is
def prefilter_import():
db_columns = Contact.__table__.columns.keys()
filename = request.cookies.get('workfile')
with open(filename) as fh:
reader = csv.DictReader(fh)
file_columns = reader.fieldnames
form = ImportFilterForm()
for col in db_columns:
new_row = form.rows.append_entry()
new_row.use.label = col
new_row.sel.choices = file_columns
input_file = request.cookies.get('input_file')
return render_template('filter_import.html', form=form, filename=input_file)
def postfilter_import():
form = ImportFilterForm()
db_columns = Contact.__table__.columns.keys()
filename = request.cookies.get('workfile')
with open(filename) as fh:
reader = csv.DictReader(fh)
file_columns = reader.fieldnames
missing_columns = db_columns
extra_columns = file_columns
mappings = dict()
for i, row in enumerate(form.rows):
if row.use.data: # Problem arises here
mappings[row.use.data] = row.sel.data
for key, value in mappings.items():
missing_columns.remove(key) # Problem manifests here
extra_columns.remove(value)
I'm trying to create a dict mapping the values of the checkboxes to the values of the selects, but I'm only getting True and False for the checkboxes, even though I've verified that the checkboxes' value attributes are correctly returned as the names of the corresponding columns.
How can I get Flask/WTForms to return the text of the value attributes?