From cbf4c3497d21a8713081b6ca1241f27e3e1d4411 Mon Sep 17 00:00:00 2001 From: Ross Date: Sat, 1 Nov 2025 20:57:35 +0000 Subject: [PATCH] Ensure selection column is always placed last in the column order for improved table layout --- generic/tables.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/generic/tables.py b/generic/tables.py index dbe32f08..c20bd3e9 100644 --- a/generic/tables.py +++ b/generic/tables.py @@ -35,6 +35,28 @@ class SelectionTable(tables.Table): # locate tables that support row selection. attrs = {"class": "table js-row-selectable", "data-selection-name": "selection"} + def __init__(self, *args, **kwargs): + # Ensure selection column is always placed last in the column order + # so that table actions/controls appear to the right. We attempt to + # reorder columns after initialization; if django-tables2 provides + # `reorder_columns`, use it, otherwise fall back to setting + # `self.sequence`. + super().__init__(*args, **kwargs) + try: + cols = list(self.columns.names()) + if 'selection' in cols: + cols.remove('selection') + cols.append('selection') + try: + # Preferred API when available + self.reorder_columns(cols) + except Exception: + # Fallback: set sequence attribute + self.sequence = tuple(cols) + except Exception: + # Defensive: don't break table rendering if introspection fails + pass + class SingleImageColumn(tables.Column): def render(self, value): try: