Closes #20808: Show occupying Device in Rack Position Selector (#21744)

Add a description field to the rack unit serializer containing the
occupying Device, allowing the Rack position dropdown to show Device
context while keeping the existing display value unchanged.

Co-authored-by: Martin Hauser <mhauser@netboxlabs.com>
This commit is contained in:
Laurent Stéphenne 2026-05-14 07:22:56 -04:00 committed by GitHub
parent 9b5f29af23
commit df5bc85b48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 0 deletions

View File

@ -26,7 +26,12 @@ class RackUnitSerializer(serializers.Serializer):
device = DeviceSerializer(nested=True, read_only=True)
occupied = serializers.BooleanField(read_only=True)
display = serializers.SerializerMethodField(read_only=True)
description = serializers.SerializerMethodField(read_only=True)
@extend_schema_field(OpenApiTypes.STR)
def get_display(self, obj):
return obj['name']
@extend_schema_field(OpenApiTypes.STR)
def get_description(self, obj):
return f'{obj["device"]}' if obj['device'] else None

View File

@ -716,6 +716,34 @@ class RackTestCase(APIViewTestCases.APIViewTestCase):
response = self.client.get(f'{url}?q=U10', **self.header)
self.assertEqual(response.data['count'], 2)
def test_get_rack_elevation_description_is_occupying_device_name(self):
"""
Verify occupied rack units include the occupying device in their description.
"""
rack = Rack.objects.first()
self.add_permissions('dcim.view_rack', 'dcim.view_device')
url = reverse('dcim-api:rack-elevation', kwargs={'pk': rack.pk})
device = create_test_device(
name='Device A',
site=rack.site,
rack=rack,
position=40,
face=DeviceFaceChoices.FACE_FRONT,
)
# Retrieve all units
response = self.client.get(url, **self.header)
self.assertHttpStatus(response, status.HTTP_200_OK)
occupied_unit = next(unit for unit in response.data['results'] if unit['name'] == 'U40')
self.assertEqual(occupied_unit['device']['id'], device.pk)
self.assertEqual(occupied_unit['description'], f'{device}')
unoccupied_unit = next(unit for unit in response.data['results'] if unit['name'] == 'U39')
self.assertEqual(unoccupied_unit['device'], None)
self.assertEqual(unoccupied_unit['description'], None)
def test_get_rack_elevation_svg(self):
"""
GET a single rack elevation in SVG format.