我尝试在表单中使用 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()
但我总是有:不是一个有效的选择
有人知道如何在 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()
但如果有人知道更优雅的解决方案,欢迎您!
关于python - 不是有效的选择 : Can't select ReferenceProperty value at SelectField wtform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6426196/