Search code examples
pythonflaskwtformspy2neo

Choices validation in WTForms does not update when database does


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.


Solution

  • 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.