[Django]-How to sort Django form fields automatically

2👍

You can call the BaseForm‘s order_fields() method:

my_form_obj = MyForm()
my_form_obj.order_fields(['field6', 'field3'])

Alternatively the Meta.fields in a ModelForm are ordered. The form fields will be generated in the same order that they are defined there:

class MyForm(forms.ModelForm):

    class Meta:
        model= MyModel
        fields = ['field6', 'field3', ...]
👤elyas

Leave a comment