0
我想擁有一個充滿燒瓶形式的SQLAlchemy的請求的結果選擇字段,但我不斷收到一個「類型錯誤:‘NoneType’對象不是可迭代麻煩與QuerySelectField在燒瓶WTF
我(how to use QuerySelectField in flask?),但它並沒有幫助我解決爲什麼我的查詢(選擇= Course.query.all())回來沒有問題 - 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
我的forms.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()])
和HTML格式:
{% 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 %}