-1

Is is possible to have a WTForm in which a drop down selection box (SelectField or QuerySelectField) displays a concatentated display value?

For example, your database may contain:

ID=1, FirstName=John, LastName=Smith
ID=2, FirstName=Kim, LastName=Johnson

so the generated HTML code would be something like:

<select name="userid">
    <option value="1">John Smith</option>
    <option value="2">Kim Johnson</option>
</select>

The display value is the concatenated value of FirstName + LastName. The unique identifier in the database will be the ID.

Akshay Anurag
  • 724
  • 1
  • 8
  • 27
Juetron
  • 31
  • 4

1 Answers1

1

You need a dynamically set SelectField:

form.userid.choices = [(item.id, item.firstname+' '+item.lastname) for item in 
session.query(ModelName).all()]
Akshay Anurag
  • 724
  • 1
  • 8
  • 27
  • Had to tweak it slightly to get it to work in my code. But your suggestion worked for me. Thank you. I was originally trying to use the QuerySelectField but that led to some bizarre behaviour - which I still cannot explain. – Juetron Jun 12 '19 at 10:52
  • This also helped with the detail. https://stackoverflow.com/questions/46921823/dynamic-choices-wtforms-flask-selectfield – Juetron Jun 12 '19 at 10:55