From 01267e69393a5133ebe20c6595f9ef9993b9ebf9 Mon Sep 17 00:00:00 2001 From: Oliver Zander Date: Tue, 14 Oct 2025 11:15:50 +0200 Subject: [PATCH] simplified field ordering --- input/forms.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/input/forms.py b/input/forms.py index 8c2b6a5..ee44358 100755 --- a/input/forms.py +++ b/input/forms.py @@ -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):