我正在尝试使用创建注册表单烧瓶wtf, and wtforms-炼金术在此表单上,我尝试创建选择字段,其值是从我的模型中查询的。
但我总是收到这个错误:
ValueError: too many values to unpack (expected 2)
this my code:
from flask_wtf import FlaskForm
from wtforms_sqlalchemy.fields import QuerySelectField
def choose_domicile():
return Domicile.query
class RegisterForm(FlaskForm):
name = StringField('Name', validators=[DataRequired()])
domicile = QuerySelectField(u'Domicile', query_factory=choose_domicile)
@app.route('/signup', methods=['GET', 'POST'])
def signup():
form = RegisterFormView()
try:
if form.validate_on_submit():
new_user = Data(name=form.name.data, domicile=form.domicile.data)
db.session.add(new_user)
db.session.commit()
return "Success"
except:
return "Failed"
return render_template('signup.html', form=form)
这是我的注册.html:
{% block content %}
<div class="container">
<form class="form-signin" method="POST" action="/signup">
<h2 class="form-signin-heading">Sign Up</h2>
{{ form.hidden_tag() }}
{{ wtf.form_field(form.name) }}
{{ form.domicile }}
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign Up</button>
</form>
</div>
{% endblock %}
我的软件包版本是烧瓶-wtf==0.14.2 and wtforms-alchemy==0.16.7
我试着遵循这个为什么 Flask WTForms 和 WTForms-SQLAlchemy QuerySelectField 会产生太多值而无法解压? https://stackoverflow.com/questions/48353190/why-does-flask-wtforms-and-wtforms-sqlalchemy-queryselectfield-produce-too-many,但我仍然很困惑如何做到这一点。