【问题标题】:wtforms sqlalchemy selectfield generating string instead of boolean valuewtforms sqlalchemy 选择字段生成字符串而不是布尔值
【发布时间】:2020-09-06 22:57:45
【问题描述】:

我正在尝试将记录插入数据库,但 sqlalchemy 给了我错误:

sqlalchemy.exc.StatementError: (builtins.TypeError) Not a boolean value: 'True'

我正在使用这样的 wtforms 在选择字段中设置值:

    seeking_talent = SelectField(
        'seeking_talent', validators=[InputRequired()], coerce=bool,
        choices=[
            (True, 'Yes'),
            (False, 'No'),
        ]
    )

并像这样在我的 html 中调用它:

      <div class="form-group">
        <label for="seeking_description">What kind of music are you looking for?</label>
        {{ form.seeking_description(class_ = 'form-control', id=seeking_description, autofocus = true) }}
      </div>

我怎样才能停止收到此错误?

【问题讨论】:

  • 你能发布你的模型代码吗?
  • 表单的值总是字符串。你得到的是“True”,而不是True

标签: python flask sqlalchemy wtforms


【解决方案1】:

像这样使用强制:

dark_theme = SelectField(_l('Theme'), 
                choices=[('', 'Light theme'), (True, 'Dark theme')], coerce=bool)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-29
    • 2021-12-11
    • 2022-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    相关资源
    最近更新 更多