Skip to content

Commit c9c537a

Browse files
committed
Fixes #6817: Custom field columns should be removed from tables upon their deletion
1 parent 1be748b commit c9c537a

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

docs/release-notes/version-3.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
### Bug Fixes
1313

1414
* [#6433](https://github.com/netbox-community/netbox/issues/6433) - Fix bulk editing of child prefixes under aggregate view
15+
* [#6817](https://github.com/netbox-community/netbox/issues/6817) - Custom field columns should be removed from tables upon their deletion
1516
* [#6895](https://github.com/netbox-community/netbox/issues/6895) - Remove errant markup for null values in CSV export
1617
* [#7373](https://github.com/netbox-community/netbox/issues/7373) - Fix flashing when server, client, and browser color-mode preferences are mismatched
1718
* [#7397](https://github.com/netbox-community/netbox/issues/7397) - Fix AttributeError exception when rendering export template for devices via REST API

netbox/utilities/tables.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,18 @@ class Meta:
2929
'class': 'table table-hover object-list',
3030
}
3131

32-
def __init__(self, *args, user=None, **kwargs):
32+
def __init__(self, *args, user=None, extra_columns=None, **kwargs):
3333
# Add custom field columns
3434
obj_type = ContentType.objects.get_for_model(self._meta.model)
35-
for cf in CustomField.objects.filter(content_types=obj_type):
36-
self.base_columns[f'cf_{cf.name}'] = CustomFieldColumn(cf)
35+
cf_columns = [
36+
(f'cf_{cf.name}', CustomFieldColumn(cf)) for cf in CustomField.objects.filter(content_types=obj_type)
37+
]
38+
if extra_columns is not None:
39+
extra_columns.extend(cf_columns)
40+
else:
41+
extra_columns = cf_columns
3742

38-
super().__init__(*args, **kwargs)
43+
super().__init__(*args, extra_columns=extra_columns, **kwargs)
3944

4045
# Set default empty_text if none was provided
4146
if self.empty_text is None:
@@ -50,17 +55,22 @@ def __init__(self, *args, user=None, **kwargs):
5055

5156
# Apply custom column ordering for user
5257
if user is not None and not isinstance(user, AnonymousUser):
53-
columns = user.config.get(f"tables.{self.__class__.__name__}.columns")
54-
if columns:
58+
selected_columns = user.config.get(f"tables.{self.__class__.__name__}.columns")
59+
if selected_columns:
5560
pk = self.base_columns.pop('pk', None)
5661
actions = self.base_columns.pop('actions', None)
5762

58-
for name, column in self.base_columns.items():
59-
if name in columns:
63+
for name, column in self.columns.items():
64+
if name in selected_columns:
6065
self.columns.show(name)
6166
else:
6267
self.columns.hide(name)
63-
self.sequence = [c for c in columns if c in self.base_columns]
68+
# Rearrange the sequence to list selected columns first, followed by all remaining columns
69+
# TODO: There's probably a more clever way to accomplish this
70+
self.sequence = [
71+
*[c for c in selected_columns if c in self.columns.names()],
72+
*[c for c in self.columns.names() if c not in selected_columns]
73+
]
6474

6575
# Always include PK and actions column, if defined on the table
6676
if pk:

0 commit comments

Comments
 (0)