如何将flask_sqlalchemy orm中的数据添加到WTForm FieldList?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将flask_sqlalchemy orm中的数据添加到WTForm FieldList?相关的知识,希望对你有一定的参考价值。

我想使用flask,WTForm和flask_sqlalchemy建立一个网站。有一个网页供用户通过WTForm提交信息,现在我想创建另一个页面来显示数据库中的所有数据。我想使用WTForm做到这一点。在一本书中,示例如下:

def GetSchoolFormById(school_id):
    school = orm.School.query.get(int(school_id))
    if school is None:
        return None
    schoolform = SchoolForm()
    schoolform.id.data = school.id
    schoolform.name.data = school.name
    schoolform.area_id.data = school.area_id
    schoolform.area_name = school.area.name
    schoolform.teachdesc.data = school.teachdesc
    schoolform.address.data = school.address
    schoolform.schooltype_id.data = school.schooltype_id
    schoolform.schooltype_name = school.schooltype.name
    schoolform.website.data = school.website
    schoolform.distinguish.data = school.distinguish
    schoolform.leisure.data = school.leisure
    schoolform.threashold.data = school.threashold
    schoolform.partner.data = school.partner
    schoolform.artsource.data = school.artsource
    schoolform.feedesc.data = school.feedesc
    schoolform.longitude.data = school.longitude
    schoolform.latitude.data = school.latitude
    schoolform.schoolimages = school.schoolimages
    schoolform.feature_ids.data = [x.feature_id for x in school.schoolfeatures]

    schoolform.area_id.choices = g_choices_area
    schoolform.schooltype_id.choices = g_choices_schooltype
    schoolform.feature_ids.choices = g_choices_feature
    return schoolform

但是对于我来说,我使用FieldList,所以我不知道如何像示例一样添加数据。

class ProjectForm(FlaskForm):
    project_id = HiddenField('项目ID')
    project_name = StringField('工程名称')
    project_ST = DateField('起始时间', format='%Y-%m-%d')
    project_FT = DateField('截止时间', format='%Y-%m-%d')


class ItemsForm(Form):
    item_id = HiddenField('事件ID')
    item_name = StringField('事件名称')
    pre_item = SelectMultipleField('前驱事件')
    # 暂定为string类型,加到date的%d里,具体我也不会,实现不了可以再改
    last_time = StringField('持续时间')


class MainForm(FlaskForm):
    items = FieldList(
        FormField(ItemsForm)
    )
@app.route('/view_items', methods=['GET', 'POST'])
def view_items():
    form_one = ProjectForm()
    form_two = MainForm()
    if request.method == 'GET':
        project = DBProject.query.filter_by(project_id=1).first().__dict__
        print(project)
        # 由于目前只有一个project,所以直接导入全部items数据
        items = list(DBItem.query.filter().all())

        form_one.project_id.data = project['project_id']
        form_one.project_name.data = project['project_name']
        form_one.project_ST.data = project['project_ST']
        form_one.project_FT.data = project['project_FT']

        for item in items:
            print(item)
            form_two.items.append_entry(ItemsForm(item))
    return render_template('view_items.html', form_one=form_one, form_two=form_two)

当我测试我的代码时,像这样从底部开始的第二行出现错误:

TypeError: formdata should be a multidict-type wrapper that supports the 'getlist' method

我该如何处理?我应该使用其他方法而不是WTForm将数据传递到html页面吗?

答案
None

以上是关于如何将flask_sqlalchemy orm中的数据添加到WTForm FieldList?的主要内容,如果未能解决你的问题,请参考以下文章

flask_sqlalchemy join的正确使用方法

flask_sqlalchemy

flask_sqlalchemy简单用法

使用 flask_sqlalchemy 将 HTML 表单中的日期插入 SQlite 数据库

SQLAlchemy模型中的进程字段(使用flask_sqlalchemy)

如何在 flask_sqlalchemy 中使用 PostgreSQL 的“INSERT...ON CONFLICT”(UPSERT)功能?