Andrew Kravchuk Andrew Kravchuk - 2 days ago 4
Python Question

Wtforms-alchemy field order

I'm using wtforms-alchemy in my Tornado application to render SQLAlchemy models to HTML forms like this:

class UserProfileForm(ModelForm):
class Meta:
model = models.User
only = ['username', 'first_name', 'last_name']


It works just fine, but the fields on the form are in order last_name, username, first_name, which could be kinda confusing to user. Is there a way to set specific order of generated form's fields?

Answer

I did what this post suggested, and its been working flawlessly for me.

Briefly, add a class keyword that specifies the correct ordering, then update _unbound_fields from iter with fields in correct order.

Comments