![](/img/trans.png)
[英]Python Flask WTForm SelectField with Enum values 'Not a valid choice' upon validation
[英]Not a valid choice: Can't select ReferenceProperty value at SelectField wtform
我嘗試在表單上使用ReferenceProperty進行創建/編輯條目,但是什么也沒有發生。
我有:
class Type(db.Model):
name = db.StringProperty()
class Entry(db.Model):
type = db.ReferenceProperty(Type, required=False)
class EntryForm(Form):
_type_list = []
for type in Type.all():
_type_list.append((type.key(),type.name))
type = fields.SelectField(u'Type of entry', choices = _type_list)
並編輯處理程序:
def post(self, **kwargs):
self.form = EntryForm(self.request.form)
if self.form.validate():
values = {
'type': models.Type.get_by_key_name(self.form.type.data).key(),
}
entry = Entry(**values)
entry.put()
但我總是有:不是一個有效的選擇
enyone知道如何在wtforms SelectField中使用ReferenceProperty嗎?或者您可能對此有工作示例?
我通過Form類的此更改解決了我的問題:
class EntryForm(Form):
_type_list = []
for type in Type.all():
_type_list.append((type.key().id(),type.name))
type = fields.SelectField(u'Type of entry', choices = _type_list, coerce=int)
並編輯處理程序:
def post(self, **kwargs):
self.form = EntryForm(self.request.form)
if self.form.validate():
values = {
'type': models.Type.get_by_id(self.form.type.data),
}
entry = Entry(**values)
entry.put()
但是,如果有人知道更優雅的解決方案,歡迎您!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.