在选择上设置默认设置会删除为填充表单而传入的设置 [英] Setting a default on a select removes the settings passed in to populate the form

查看:144
本文介绍了在选择上设置默认设置会删除为填充表单而传入的设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码可以单独正常工作.我的意思是,当我设置默认值tag并调用process()时,应填充表格的所有其他数据均已删除.在这种情况下,默认值为ok,但其他字段为空.

This code works fine separately. What I mean is when I set the default tag and call process() all the other data that should populate the form have been removed. In this case the default is ok, but the other fields are empty.

form = ReviewForm(**populate_form)
form.tags.default = '1'
form.process()

因此,似乎process清除了**populate_form值.我需要填充所有字段,然后为选择设置默认值.

So, it seems like process cleans the **populate_form values out. I need to populate all fields and then set the default for the select.

推荐答案

来自 WTForms文档:

由于BaseForm不会在实例化时获取其数据,因此必须调用此方法才能将表格数据提供给所包含的字段.不建议在调用过程之前访问该字段的数据.

Since BaseForm does not take its data at instantiation, you must call this to provide form data to the enclosed fields. Accessing the field’s data before calling process is not recommended.

但是Form子类允许您将对象作为kwargs传入,这就是您在此处所做的.但是,查看源代码,它看起来像这里发生的是__init__正在调用process来处理字段,所以我认为有一些重载正在进行.

But the Form subclass allows you to pass in objects as kwargs, which you are doing here. However, looking at the source, it looks like what is happening here is that __init__ is calling process to process the fields, so I think there is some overriding going on.

无论如何,我认为这里缺少的是默认值应该在定义时定义,而不是在实例化之后定义.查看构造以及此答案,我认为这样可以解决问题.

In any case, I think what is missing here is that the default value should be defined upon definition, not after instantiation. Look at the construction as well as this answer, and I think that will clear things up.

这篇关于在选择上设置默认设置会删除为填充表单而传入的设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆