For Cable API add PK for cable terminations to serializer

This commit is contained in:
Daniel Sheppard 2024-08-17 21:08:39 -05:00
parent 277b7039d8
commit eb4ef18d61
2 changed files with 23 additions and 2 deletions

View File

@ -16,14 +16,27 @@ __all__ = (
'CableSerializer',
'CableTerminationSerializer',
'CabledObjectSerializer',
'GenericObjectTerminationSerializer',
'TracedCableSerializer',
)
class GenericObjectTerminationSerializer(GenericObjectSerializer):
id = serializers.IntegerField(required=False)
def to_representation(self, instance):
data = super().to_representation(instance)
termination = instance.cable_terminations.first()
if termination:
data['id'] = termination.pk
return data
class CableSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='dcim-api:cable-detail')
a_terminations = GenericObjectSerializer(many=True, required=False)
b_terminations = GenericObjectSerializer(many=True, required=False)
a_terminations = GenericObjectTerminationSerializer(many=True, required=False)
b_terminations = GenericObjectTerminationSerializer(many=True, required=False)
status = ChoiceField(choices=LinkStatusChoices, required=False)
tenant = TenantSerializer(nested=True, required=False, allow_null=True)
length_unit = ChoiceField(choices=CableLengthUnitChoices, allow_blank=True, required=False, allow_null=True)

View File

@ -2038,6 +2038,14 @@ class CableTest(APIViewTestCases.APIViewTestCase):
},
]
def test_cable_termination_pk_exist(self):
self.add_permissions('dcim.view_cable')
cable = Cable.objects.first()
url = reverse('dcim-api:cable-detail', kwargs={'pk': cable.pk})
response = self.client.get(url, {}, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_200_OK)
self.assertIsNotNone(response.data['a_terminations'][0]['id'])
class ConnectedDeviceTest(APITestCase):