on_form_prefill delete all fields values if it executes form.process

≯℡__Kan透↙ 提交于 2019-12-09 03:40:29

I solve this problem by process_data

def on_form_prefill(self, form, id):
    form.clan.choices = [(1, "first"), (2, "second")]
    form.clan.process_data(2)

I hope this can help you

I have a very similar problem and made it work by re-processing only the changed field. Also I needed to pass the currently set value in again (else it would be always set to the default):

from wtforms.utils import unset_value

class CustomView:
    def on_form_prefill(self, form, id):
        form.clan.choices = [(1, "first"),(2,"second")]
        form.clan.default = 2
        form.clan.process(None, form.clan.data or unset_value)

NOTE: As on_form_prefill is only called in the edit_view and not in the create_view this does only work with existing instances.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!