Repository: nautobot/nautobot

Issue:
Sort by Status on dcim/device-bays
### Environment
* Nautobot version (Docker tag too if applicable): v1.5.21
* Python version: Python 3.10.12
* Database platform, version: PostgreSQL 13.8
* Middleware(s): None

When accessing Device Bays and sorting, sorting by status does not work.
### Steps to Reproduce
1. Navigate to Devices > Device bays (`dcim/device-bays/?q=&sort=name`)
2. Select "Status" at the top of the page

<!-- What did you expect to happen? -->
### Expected Behavior
Sort the device bays by status

<!-- What happened instead? -->
### Observed Behavior
Django Error
![image](https://github.com/nautobot/nautobot/assets/17132988/f393142e-13bf-4995-9bce-2d92c7d3295c)

Sort by Status on dcim/device-bays
### Environment
* Nautobot version (Docker tag too if applicable): v1.5.21
* Python version: Python 3.10.12
* Database platform, version: PostgreSQL 13.8
* Middleware(s): None

When accessing Device Bays and sorting, sorting by status does not work.
### Steps to Reproduce
1. Navigate to Devices > Device bays (`dcim/device-bays/?q=&sort=name`)
2. Select "Status" at the top of the page

<!-- What did you expect to happen? -->
### Expected Behavior
Sort the device bays by status

<!-- What happened instead? -->
### Observed Behavior
Django Error
![image](https://github.com/nautobot/nautobot/assets/17132988/f393142e-13bf-4995-9bce-2d92c7d3295c)



Discussion:
The "status" column is not a field directly on `DeviceBay` it's instead derived from the DeviceBay's `installed_device.status`. A naive fix would be to simply disable sorting on this column, but it's probably fixable more completely by changing the column to use an appropriate `Accessor` to retrieve the nested value.
The "status" column is not a field directly on `DeviceBay` it's instead derived from the DeviceBay's `installed_device.status`. A naive fix would be to simply disable sorting on this column, but it's probably fixable more completely by changing the column to use an appropriate `Accessor` to retrieve the nested value.

Resolving patch:
diff --git a/nautobot/dcim/tables/devices.py b/nautobot/dcim/tables/devices.py
--- a/nautobot/dcim/tables/devices.py
+++ b/nautobot/dcim/tables/devices.py
@@ -37,15 +37,12 @@
     CONSOLESERVERPORT_BUTTONS,
     DEVICE_LINK,
     DEVICEBAY_BUTTONS,
-    DEVICEBAY_STATUS,
     FRONTPORT_BUTTONS,
     INTERFACE_BUTTONS,
     INTERFACE_IPADDRESSES,
     INTERFACE_REDUNDANCY_GROUP_INTERFACES,
     INTERFACE_REDUNDANCY_GROUP_INTERFACES_IPADDRESSES,
-    INTERFACE_REDUNDANCY_GROUP_STATUS,
     INTERFACE_REDUNDANCY_INTERFACE_PRIORITY,
-    INTERFACE_REDUNDANCY_INTERFACE_STATUS,
     INTERFACE_TAGGED_VLANS,
     LINKED_RECORD_COUNT,
     PATHENDPOINT,
@@ -772,7 +769,7 @@ class Meta(DeviceComponentTable.Meta):
 
 
 class DeviceBayTable(DeviceComponentTable):
-    status = tables.TemplateColumn(template_code=DEVICEBAY_STATUS)
+    installed_device__status = ColoredLabelColumn()
     installed_device = tables.Column(linkify=True)
     tags = TagColumn(url_name="dcim:devicebay_list")
 
@@ -783,7 +780,7 @@ class Meta(DeviceComponentTable.Meta):
             "device",
             "name",
             "label",
-            "status",
+            "installed_device__status",
             "installed_device",
             "description",
             "tags",
@@ -793,7 +790,7 @@ class Meta(DeviceComponentTable.Meta):
             "device",
             "name",
             "label",
-            "status",
+            "installed_device__status",
             "installed_device",
             "description",
         )
@@ -813,7 +810,7 @@ class Meta(DeviceComponentTable.Meta):
             "pk",
             "name",
             "label",
-            "status",
+            "installed_device__status",
             "installed_device",
             "description",
             "tags",
@@ -823,7 +820,7 @@ class Meta(DeviceComponentTable.Meta):
             "pk",
             "name",
             "label",
-            "status",
+            "installed_device__status",
             "installed_device",
             "description",
             "actions",
@@ -984,11 +981,8 @@ class InterfaceRedundancyGroupAssociationTable(BaseTable):
     priority = tables.TemplateColumn(template_code=INTERFACE_REDUNDANCY_INTERFACE_PRIORITY)
     interface__device = tables.Column(linkify=True)
     interface = tables.Column(linkify=True)
-    interface__status = tables.TemplateColumn(template_code=INTERFACE_REDUNDANCY_INTERFACE_STATUS)
-    interface_redundancy_group__status = tables.TemplateColumn(
-        template_code=INTERFACE_REDUNDANCY_GROUP_STATUS,
-        verbose_name="Group Status",
-    )
+    interface__status = ColoredLabelColumn()
+    interface_redundancy_group__status = ColoredLabelColumn(verbose_name="Group Status")
     interface__ip_addresses = tables.TemplateColumn(
         template_code=INTERFACE_REDUNDANCY_GROUP_INTERFACES_IPADDRESSES,
         orderable=False,
diff --git a/nautobot/dcim/tables/template_code.py b/nautobot/dcim/tables/template_code.py
--- a/nautobot/dcim/tables/template_code.py
+++ b/nautobot/dcim/tables/template_code.py
@@ -50,17 +50,6 @@
 </a>
 """
 
-DEVICEBAY_STATUS = """
-{% if record.installed_device_id %}
-    {% load helpers %}
-    <span class="label" style="color: {{ record.installed_device.status.color|fgcolor }}; background-color: #{{ record.installed_device.status.color }}">
-        {{ record.installed_device.get_status_display }}
-    </span>
-{% else %}
-    <span class="label label-default">Vacant</span>
-{% endif %}
-"""
-
 INTERFACE_IPADDRESSES = """
 {% for ip in record.ip_addresses.all %}
     <a href="{{ ip.get_absolute_url }}">{{ ip }}</a> (<a href="{{ ip.parent.namespace.get_absolute_url }}">{{ ip.parent.namespace }}</a>)<br />
@@ -77,15 +66,6 @@
 {% endfor %}
 """
 
-INTERFACE_REDUNDANCY_GROUP_STATUS = """
-{% load helpers %}
-<span class="label"
-    style="color: {{ record.interface_redundancy_group.status.color|fgcolor }};
-    background-color: #{{ record.interface_redundancy_group.status.color }}">
-    {{ record.interface_redundancy_group.get_status_display }}
-</span>
-"""
-
 INTERFACE_REDUNDANCY_INTERFACE_PRIORITY = """
 {% load helpers %}
 <span class="badge badge-default">
@@ -93,13 +73,6 @@
 </span>
 """
 
-INTERFACE_REDUNDANCY_INTERFACE_STATUS = """
-{% load helpers %}
-<span class="label" style="color: {{ record.interface.status.color|fgcolor }}; background-color: #{{ record.interface.status.color }}">
-    {{ record.interface.get_status_display }}
-</span>
-"""
-
 INTERFACE_TAGGED_VLANS = """
 {% if record.mode == 'tagged' %}
     {% for vlan in record.tagged_vlans.all %}


Test patch: