Last Updated: February 25, 2016
· tijs
Starbird crop2

Re-ordering Django form fields

Luckily this is easy, although undocumented, in Django. Simple set the keyOrder in the forms init method like so:

class MyForm(EditForm):
    name = forms.CharField()
    bio = forms.TextField()
    email = forms.CharField()

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields.keyOrder = ['name', 'email', 'bio']

In this example it's a bit trivial but if your fields are coming from different places, like your model plus some extra from your form, this can be really useful.

Say Thanks