Choices validation in WTForms does not update when database does

点点圈 提交于 2019-12-06 02:49:17

问题


I understand the SelectField method in WTForms takes can argument choices which has the form...

choices=[("value1", "display of value 1"), ("value2", "display of value 2")]

I need to populate my choices based on a call to the database. I'm using neo4j as my backend, so I can't use modelforms or the other built-in solutions for populating data in a form.

def get_list_of_things():
    query = 'start n = node:things("*:*") return ID(n), n.display_name;'
    t = cypher.execute(cdb, query)[0]
    for x in t:
        x[0] = str(x[0])
        x[1] = str(x[1])
    things = list(tuple(x) for x in t)
    return things

class SelectAThing(Form):
    thingID = SelectField('Thing name', choices=get_list_of_things())

Running choices = get_list_of_things() does produce a valid list of choices, great, and this basically works.

However, it doesn't ever seem to update the list of things even when the database does and I return to that form later. If I add things to the db and return, I still see the first list of things.


回答1:


Yup - you got it. WTForms is a little unintuitive that way.

By the way, if you're pulling choices out of a SQLAlchemy database (and you're using Flask), check out the QuerySelectField addon:

http://wtforms.simplecodes.com/docs/0.6.1/ext.html#module-wtforms.ext.sqlalchemy

from wtforms.ext.sqlalchemy.fields import QuerySelectField

def all_employees():
  return Employee.query

class BugReportForm(Form):
  description = TextField(u"Notes")
  # The get_label will show the "name" attribute of the Employee model
  assign_to = QuerySelectField(u'Assign to',
                           get_label=u"name",
                           query_factory=all_employees)

That'll give you a Select field with the names of everybody.

Bonus: when you access BugReportForm.assign_to.data in the view code, it'll return the Employee object (not the id). It's handy.




回答2:


No dummy, just don't put it in the class. Put it in the view code.

@app.route('/route')
def routename()
    form = SelectAThing()
    form.orgid.choices=get_joinable_orgs()

I found this tricky because I didn't realize I could assign to form like it was a regular python object after initializing it in the view.



来源:https://stackoverflow.com/questions/13781616/choices-validation-in-wtforms-does-not-update-when-database-does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!