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): def __init__(self, *args, **kwargs):
""" """
Override the default form initialization to reorder fields. Override the default form initialization to reorder fields.
The method: The method:
1. Collects all existing field names. 1. Put the 'head' fields first.
2. Puts the 'head' fields first (if they exist in this form). 2. Put all other fields in the middle.
3. Keeps all other fields in the middle. 3. Put the 'tail' fields last.
4. Puts the 'tail' fields last (if they exist in this form).
Non-existing fields are ignored by `order_fields`.
""" """
super().__init__(*args, **kwargs) 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 self.order_fields([
head = [f for f in self.field_order_head if f in self.fields] *self.field_order_head,
tail = [f for f in self.field_order_tail if f in self.fields] *[field for field in self.fields if field not in ordered],
*self.field_order_tail,
# 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)
class ExternForm(FdbForm): class ExternForm(FdbForm):