how to sort wtforms fields

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import collections
from wtforms import validators
from wtforms_alchemy import ModelForm
class UserForm(ModelForm):
def __init__(self, *args, **kwargs):
super(UserForm, self).__init__(*args,**kwargs)
# magic here
fields = self._fields
self._fields = collections.OrderedDict(sorted(fields.iteritems(), key=lambda x: x[0]))
operator = StringField(
validators=[validators.DataRequired(message=u"Оператор")])
telephone = StringField(
validators=[validators.DataRequired(message=u"Телефон")]
)
class Meta:
model = Client
only = ["email", "password", "family_name", "name", "patronymic",
"town", "address", "delivery_address", "passport",
"subscription"]