Add is_primary field to MAC address REST API serializer

This commit is contained in:
Arthur 2026-06-16 14:39:55 -07:00
parent bf954f08d6
commit c3d8a2dc0f
2 changed files with 25 additions and 1 deletions

View File

@ -290,11 +290,13 @@ class MACAddressSerializer(PrimaryModelSerializer):
allow_null=True
)
assigned_object = GFKSerializerField(read_only=True)
is_primary = serializers.BooleanField(read_only=True)
class Meta:
model = MACAddress
fields = [
'id', 'url', 'display_url', 'display', 'mac_address', 'assigned_object_type', 'assigned_object_id',
'assigned_object', 'description', 'owner', 'comments', 'tags', 'custom_fields', 'created', 'last_updated',
'assigned_object', 'is_primary', 'description', 'owner', 'comments', 'tags', 'custom_fields', 'created',
'last_updated',
]
brief_fields = ('id', 'url', 'display', 'mac_address', 'description')

View File

@ -3880,3 +3880,25 @@ class MACAddressTestCase(APIViewTestCases.APIViewTestCase):
'mac_address': '00:00:00:00:00:06',
},
]
def test_is_primary_field(self):
"""
The read-only is_primary field should reflect whether the MAC address is the primary on its interface.
"""
self.add_permissions('dcim.view_macaddress')
primary_mac = MACAddress.objects.get(mac_address='00:00:00:00:00:01')
non_primary_mac = MACAddress.objects.get(mac_address='00:00:00:00:00:02')
# Designate one MAC address as the primary on its interface
interface = primary_mac.assigned_object
interface.primary_mac_address = primary_mac
interface.save()
url = reverse('dcim-api:macaddress-detail', kwargs={'pk': primary_mac.pk})
response = self.client.get(url, **self.header)
self.assertTrue(response.data['is_primary'])
url = reverse('dcim-api:macaddress-detail', kwargs={'pk': non_primary_mac.pk})
response = self.client.get(url, **self.header)
self.assertFalse(response.data['is_primary'])