在Flask-WTF中使用QuerySelectField时出现问题

时间:2015-06-01 00:52:41

标签: python flask flask-wtforms

我正在尝试以烧瓶形式填充sqlalchemy请求结果的select字段,但我一直得到一个" TypeError:' NoneType'对象不可迭代

我看过这个答案(how to use QuerySelectField in flask?),但它没有帮我解决为什么我的查询(choices = Course.query.all())没有回来的问题 - 我认为这是这个问题。

我的models.py:

from flask_wtf import Form
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from wtforms import StringField,
from wtforms.validators import DataRequired, Length, Email, 
from project.models import Course

def select_group():
        choices = Course.query.all()
        form = EnrollmentForm(obj=choices)
        form.product.choices = [(c.id, c.course_name) for 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()])

我的forms.py

{% 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 %}

和html表单:

sudo pip install

1 个答案:

答案 0 :(得分:0)

明显的致盲闪现:

我的助手功能没有产生回报.....

def select_group():
        return Course.query.all()