python - 不是有效的选择 : Can't select ReferenceProperty value at SelectField wtform

标签 python google-app-engine tipfy wtforms

我尝试在表单中使用 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/

相关文章:

python - 根据前几年的数据计算 Pandas 数据框行的百分位数

python - 如何访问 azureml FileDataSet 中的文件(图像)?

google-app-engine - 如何让 Tipfy 应用程序接受自己的身份验证、OpenID 等?

python - Tipfy & Jinja : Creating a logout URL for every page

python - 通过 pyenv 安装 python 时缺少“_ctypes”模块

python - 调整 python 脚本来替换/转义输入数据的特定字符

java - 单元测试 App 引擎 - Eclipse - Maven

java - 如何使用 Java 在 GAE 中设置环境变量 - 不在主代码中公开?或其他隐藏应用程序使用的 key 的方法?

google-app-engine - 测试站点上达到了 Google App Engine 配额。我究竟做错了什么?

google-app-engine - tipfy nosetest ImportError : No module named fancy_urllib