simplified field ordering

This commit is contained in:
Oliver Zander 2025-10-14 11:15:50 +02:00
parent 29c4cdd8ac
commit 01267e6939
1 changed files with 13 additions and 14 deletions

View File

@ -68,25 +68,24 @@ class CommonOrderMixin(forms.Form):
def __init__(self, *args, **kwargs):
"""
Override the default form initialization to reorder fields.
The method:
1. Collects all existing field names.
2. Puts the 'head' fields first (if they exist in this form).
3. Keeps all other fields in the middle.
4. Puts the 'tail' fields last (if they exist in this form).
1. Put the 'head' fields first.
2. Put all other fields in the middle.
3. Put the 'tail' fields last.
Non-existing fields are ignored by `order_fields`.
"""
super().__init__(*args, **kwargs)
existing = list(self.fields.keys())
ordered = {*self.field_order_head, *self.field_order_tail}
# Select only fields that actually exist in this form
head = [f for f in self.field_order_head if f in self.fields]
tail = [f for f in self.field_order_tail if f in self.fields]
# All other fields that are not explicitly in head or tail
middle = [f for f in existing if f not in (*head, *tail)]
# Apply the new order to the form fields
self.order_fields(head + middle + tail)
self.order_fields([
*self.field_order_head,
*[field for field in self.fields if field not in ordered],
*self.field_order_tail,
])
class ExternForm(FdbForm):