Вопрос

First question: dynamic input into a SelectField (choices), in my Database (sqlite, with SqlAlchemy) I have a Table, and from this table I wont all entries in the choices from the SelectField. As the selected result I need the ID from the entry.

foo_id = SelectField('Label', choices=[Foo.query.all()])

Second question: If I put this into the SelectField:

foo_id = SelectField('Foo', choices=[(1, 'Foo 1'), (2, 'Foo 2')])

Every time:

Not a valid choice

What goes on with the validation?

Thanks for your time, have a nice day!

Это было полезно?

Решение

Two answers:

  1. Use wtforms.ext.sqlalchemy.fields.QuerySelectField

  2. Add a callable coerce argument that will coerce the strings you get back from the browser:

    SelectField('Foo', coerce=int, choices=[(1, 'Foo 1'), (2, 'Foo 2')])
    
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top