Add a table form override and add a table column remap option

This commit is contained in:
Daniel Sheppard 2024-06-12 23:01:33 -05:00
parent fd38255d31
commit 30e653197e
2 changed files with 14 additions and 3 deletions

View File

@ -162,14 +162,22 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin):
# Render the objects table
table = self.get_table(self.queryset, request, has_bulk_actions)
# Check for filterset_form on this view, if a form exists
# * Apply to context for use by the filter form tab and initialize the form
# Check for filterset_form(s) on this view and/or the table, if a form exists:
# * If both exist, initialize both
# * If a filterset form for the table exists, only initialize the table filterset_form
# * If a filterset form exists for the view, initialize the filterset form
# * Apply to the table for use by the table and initialize a separate instance of the form for use by the table
# column filters
# * Otherwise set to None
if self.filterset_form:
if self.filterset_form and table.filterset_form:
filterset_form = self.filterset_form(request.GET)
table.filterset_form = table.filterset_form(request.GET)
elif self.filterset_form and not table.filterset_form:
filterset_form = self.filterset_form(request.GET)
table.filterset_form = self.filterset_form(request.GET)
elif not self.filterset_form and table.filterset_form:
filterset_form = None
table.filterset_form = table.filterset_form(request.GET)
else:
filterset_form = None
table.filterset_form = None

View File

@ -36,6 +36,9 @@ def getfield(form, fieldname):
@register.filter()
def get_filter_field(form, fieldname):
# Check for a table form column map attribute and use that to map form fields if set
if hasattr(form, '_table_form_column_map') and form._table_form_column_map.get(fieldname):
return getfield(form, form._table_form_column_map.get(fieldname))
return getfield(form, f'{fieldname}') or getfield(form, f'{fieldname}_id')