mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-23 04:22:01 -06:00
For Cable API add PK for cable terminations to serializer
This commit is contained in:
parent
277b7039d8
commit
eb4ef18d61
@ -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)
|
||||
|
@ -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):
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user