[Answer]-Forms.modelform ordering?

1👍

✅

ordering is not a valid option for a model form’s Meta class, so specifying it won’t do anything.

If you always want to order the model by a particular field, you can simply set ordering in the model’s Meta class. This will affect the ordering in other places e.g. in the django admin.

class Operation(models.Model):
    # field definitions
    class Meta:
        ordering = ('date',)

If you only want to change the ordering for this formset, provide a custom queryset when you initialize it.

OperationFormSet = modelformset_factory(Operation, form=OperationCategoryOnlyForm, queryset=Operation.objects.order_by('date'))

Leave a comment