pythondeformcolanderkotti

How to re-order colander fields in a form?


I used form inheritance to create a new form, for instance:

class MyForm(ParentForm):
    employment_date = colander.SchemaNode(
        colander.Date(),
        title=_(u'Employment Date')
    )

Lets say the order of the ParentForm fields is

I want the new field, employment_date to be inserted after the email field, i.e.

I want to achieve this without redefining the fields in my schema.


Solution

  • You need to use the insert_before argument when adding your schemaNode object (you'll have to reference 'biography' as there is no insert_after argument to use with email):

    class MyForm(ParentForm):
        employment_date = colander.SchemaNode(
            colander.Date(),
            title=_(u'Employment Date'),
            insert_before='biography',
        )
    

    Colander schemaNode docs