wtforms Form class subclassing and field ordering

后端 未结 6 701
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-02 13:58

I have a UserForm class:

class UserForm(Form):
    first_name = TextField(u\'First name\', [validators.Required()])
    last_name = TextField(u\'Last name\', [va         


        
6条回答
  •  误落风尘
    2021-02-02 14:32

    I have combined two answers into following snippet:

    def __iter__(self):
        ordered_fields = collections.OrderedDict()
    
        for name in getattr(self, 'field_order', []):
            ordered_fields[name] = self._fields.pop(name)
    
        ordered_fields.update(self._fields)
    
        self._fields = ordered_fields
    
        return super(BaseForm, self).__iter__()
    

    It's iter on BaseForm that each of my form is child of. Basically everything that is defined in field_order goes in that order, rest of the fields are rendered as-is.

提交回复
热议问题