when dynamically creating choices selectfield in wtforms, (u'choice',) rendered in dropdown list. suspect unicode, no idea how correct string.
for example
form.group_id_name.choices = [(row, row) row in db.session.query(entry.group_id_name).distinct()]
in forms have
group_id_name = selectfield('group_id_name')
i render
<select id="group_id_name" name="group_id_name"><option value="choice1">choice1</option><option value="choice2">choice2</option></select>
instead get
<select id="group_id_name" name="group_id_name"><option value="(u'choice1',)">(u'choice1',)</option><option value="(u'choice2',)">(u'choice2',)</option></select>
it's not unicode.
query()
returns sequence of column values each row. query 1 column in length-1-tuple.
when implicitly convert tuple string part of template python code representation of tuple, looks (somevalue,)
.
you want include string value of column in template, should access first element of sequence, eg:
form.group_id_name.choices = [(row[0], row[0]) row in db.session.query(entry.group_id_name).distinct()]
or using unpacking assignment:
form.group_id_name.choices = [(name, name) (name,) in db.session.query(entry.group_id_name).distinct()]
Comments
Post a Comment