Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 507
Mittenchops Avatar asked Dec 08 '12 20:12

Mittenchops


People also ask

Which of the following validation can be used to compare values of two form fields?

You can compare values across different fields using a record validation rule.

What is StringField?

StringField (default field arguments)[source] This field is the base for most of the more complicated fields, and represents an <input type="text"> .

What is flask validator?

Validators are applied to the Name and Email fields. Given below is the Flask application script (formexample.py). from flask import Flask, render_template, request, flash from forms import ContactForm app = Flask(__name__) app. secret_key = 'development key' @app.


2 Answers

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.

like image 198
Rachel Sanders Avatar answered Sep 30 '22 07:09

Rachel Sanders


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.

like image 45
Mittenchops Avatar answered Sep 30 '22 07:09

Mittenchops