即使使用 coerce=ObjectId,也不是动态 Select 字段 WTFORMS 的有效选择
Not a Valid Choice for Dynamic Select Field WTFORMS even with coerce=ObjectId
我正在使用 MongoDB 作为后端数据库的烧瓶网站。
我遇到无法解决的错误。
我正在创建一个带有动态选择的 Flask WTF RadioField(从 MongoDB 集合中查询,其中包含入院患者列表):
`class PatientDevConnectForm(Form):
patient = RadioField('Select patient',
choices=[],
coerce=ObjectId,
validators=[DataRequired()])
submit = SubmitField("Connect")`
查看函数是这样的:
def patientdev_connect():
form = PatientDevConnectForm()
if form.validate_on_submit():
print("I am here")
return render_template("home.html")
else:
print(form.errors)
print(form.patient.data)
print(type(form.patient.data))
pats = mongo.db.patients
patients = pats.find()
form.patient.choices = [(patient['_id'], patient['name']) for
patient in patients]
return render_template("patientdev_connect.html", form=form)
我的HTML是这样的:
<form method="POST" action="">
{{ form.hidden_tag() }}
<fieldset class="form-group">
<div class="form-group">
Select: {{ form.patient }}
</div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outliner-info") }}
</div>
我可以毫无问题地渲染表单,还可以看到各种选择。当我单击 (select) RadioField 选项并点击 "Submit" 按钮时,我得到以下打印 indicating validate_on_submit()
failed:
{'patient': [u'Not a valid choice']}
5b433324a02d9b4bc99a106b
<class 'bson.objectid.ObjectId'>
我面临的情况与以下问题非常相似,但建议的解决方案对我不起作用。
Not a Valid Choice for Dynamic Select Field WTFORMS
您的问题可能是这两件事之一;最简单的(但我不认为这是一个)是你的验证不知道验证之前的选择所以试试这个:
def patientdev_connect():
form = PatientDevConnectForm()
# this attaches the choices to the form before validation but may overwrite..
pats = mongo.db.patients
patients = pats.find()
form.patient.choices = [(patient['_id'], patient['name']) for
patient in patients]
if form.validate_on_submit():
print("I am here")
return render_template("home.html")
else:
print(form.errors)
print(form.patient.data)
print(type(form.patient.data))
return render_template("patientdev_connect.html", form=form)
正如我评论的那样,仍然可能会失败,并且没有测试我无法确认。您可能希望以这种方式构建动态表单:
def PatientDevConnectForm(**dynamic_kwargs):
class PlaceholderForm(Form):
patient = RadioField('Select patient',
choices=[],
coerce=ObjectId,
validators=[DataRequired()])
submit = SubmitField("Connect")
pats = mongo.db.patients
patients = pats.find()
PlaceholderForm.patient.choices = [(patient['_id'], patient['name']) for
patient in patients]
return PlaceholderForm()
我正在使用 MongoDB 作为后端数据库的烧瓶网站。 我遇到无法解决的错误。
我正在创建一个带有动态选择的 Flask WTF RadioField(从 MongoDB 集合中查询,其中包含入院患者列表):
`class PatientDevConnectForm(Form):
patient = RadioField('Select patient',
choices=[],
coerce=ObjectId,
validators=[DataRequired()])
submit = SubmitField("Connect")`
查看函数是这样的:
def patientdev_connect():
form = PatientDevConnectForm()
if form.validate_on_submit():
print("I am here")
return render_template("home.html")
else:
print(form.errors)
print(form.patient.data)
print(type(form.patient.data))
pats = mongo.db.patients
patients = pats.find()
form.patient.choices = [(patient['_id'], patient['name']) for
patient in patients]
return render_template("patientdev_connect.html", form=form)
我的HTML是这样的:
<form method="POST" action="">
{{ form.hidden_tag() }}
<fieldset class="form-group">
<div class="form-group">
Select: {{ form.patient }}
</div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outliner-info") }}
</div>
我可以毫无问题地渲染表单,还可以看到各种选择。当我单击 (select) RadioField 选项并点击 "Submit" 按钮时,我得到以下打印 indicating validate_on_submit()
failed:
{'patient': [u'Not a valid choice']}
5b433324a02d9b4bc99a106b
<class 'bson.objectid.ObjectId'>
我面临的情况与以下问题非常相似,但建议的解决方案对我不起作用。
Not a Valid Choice for Dynamic Select Field WTFORMS
您的问题可能是这两件事之一;最简单的(但我不认为这是一个)是你的验证不知道验证之前的选择所以试试这个:
def patientdev_connect():
form = PatientDevConnectForm()
# this attaches the choices to the form before validation but may overwrite..
pats = mongo.db.patients
patients = pats.find()
form.patient.choices = [(patient['_id'], patient['name']) for
patient in patients]
if form.validate_on_submit():
print("I am here")
return render_template("home.html")
else:
print(form.errors)
print(form.patient.data)
print(type(form.patient.data))
return render_template("patientdev_connect.html", form=form)
正如我评论的那样,仍然可能会失败,并且没有测试我无法确认。您可能希望以这种方式构建动态表单:
def PatientDevConnectForm(**dynamic_kwargs):
class PlaceholderForm(Form):
patient = RadioField('Select patient',
choices=[],
coerce=ObjectId,
validators=[DataRequired()])
submit = SubmitField("Connect")
pats = mongo.db.patients
patients = pats.find()
PlaceholderForm.patient.choices = [(patient['_id'], patient['name']) for
patient in patients]
return PlaceholderForm()