python - Trouble with QuerySelectField in Flask-WTF -
i trying have select field filled results of sqlalchemy request in flask form keep getting "typeerror: 'nonetype' object not iterable
i have looked @ answer (how use queryselectfield in flask?) , has not me solve problem of why query(choices = course.query.all()) coming none - think issue.
my models.py:
class course(db.model): __tablename__ = 'courses' id = db.column(db.integer, primary_key=true) course_name = db.column(db.string, nullable=false) course_description = db.column(db.string, nullable=true) course_location = db.column(db.string, nullable=true) start_date = db.column(db.datetime, nullable=true) end_date = db.column(db.datetime, nullable=true) start_time = db.column(db.time, nullable=true) end_time = db.column(db.time, nullable=true) max_number_students = db.column(db.integer, default=8) spaces_left = db.column(db.integer, default=5) is_active = db.column(db.boolean, default=true) price = db.column(db.float, nullable=true) def __init__(self, course_name=none, course_description=none, course_location=none,start_date=none,end_date=none, start_time=none,end_time=none,max_number_students=none,spaces_left=none,is_active=none, price=none): self.course_name = course_name self.course_description=course_description self.course_location = course_location self.start_date = start_date self.end_date = end_date self.start_time = start_time self.end_time = end_time self.max_number_students = max_number_students self.spaces_left = spaces_left self.is_active = is_active self.price = price def __repr__(self): return "<course name {}>".format(self.course_name) class purchase(db.model): __tablename__ = 'purchases' uuid = db.column(db.string, primary_key=true) email = db.column(db.string) product_id = db.column(db.integer, db.foreignkey('courses.id')) payment_method = db.column(db.string, nullable=true, default="credit card") notes = db.column(db.string, nullable=true) date_purchased = db.column(db.datetime, nullable=false, default=func.now()) product = db.relationship(course) def __init__(self, uuid,email=none, product_id=none, product=none,payment_method=none, notes=none, date_purchased=none): self.uuid=uuid self.email=email self.product_id=product_id self.product = product self.payment_method=payment_method self.notes=notes self.date_purchased=date_purchased
my forms.py
from flask_wtf import form wtforms.ext.sqlalchemy.fields import queryselectfield wtforms import stringfield, wtforms.validators import datarequired, length, email, project.models import course def select_group(): choices = course.query.all() form = enrollmentform(obj=choices) form.product.choices = [(c.id, c.course_name) c in choices] class enrollmentform(form): email = stringfield( "email", validators=[datarequired(), email(message = none),length(min=3, max=40)] ) product= queryselectfield(query_factory=select_group) payment_method = stringfield('payment', validators=[datarequired()]) notes = stringfield('notes', validators=[datarequired()])
and html form:
{% block content %} <h1>register</h1> <br> <form class="form-signin" action = "" method = "post"> {{ form.csrf_token }} <div>{{ form.email.label }}: {{ form.email() }}</div> <div>{{ form.product.label }}: {{ form.product() }}</div> <div>{{ form.payment_method.label }}: {{ form.payment_method() }}</div> <div>{{ form.notes.label }}: {{ form.notes() }}</div> <button class="btn btn-sm btn-success" type="submit">sign in</button> </form> {% endblock %}
blinding flash of obvious:
my helper function wasn't generating return.....
def select_group(): return course.query.all()
Comments
Post a Comment