Make WTForms set field label from database model

为君一笑 提交于 2019-12-05 16:15:13

Ok, so I came up with a solution, which is a bit similar to adarsh's second comment to his answer, but overriding init of the Form used by the FormField:

class AttributeValueForm(Form):
    value = StringField('Unnamed attribute')

    def __init__(self, *args, **kwargs):
        super(AttributeValueForm, self).__init__(*args, **kwargs)
        if 'obj' in kwargs and kwargs['obj'] is not None:
            self.value.label.text = kwargs['obj'].attribute.name

You could consider using ModelForm from wtforms-alchemy

You could the define the form with the model quite easily.

Something like:

class AttributeValueForm(ModelForm):
    class Meta:
        model = Attribute
        only = (... columns that you want to include ...)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!