From 79c110df39f5958c6f77e06b87d4b9dad68fe45f Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 1 Nov 2018 10:25:30 -0400 Subject: [PATCH] Create id_string on Cable to preserve access to pk after object deletion --- netbox/dcim/models.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/netbox/dcim/models.py b/netbox/dcim/models.py index 8f6e01554..995e46bb8 100644 --- a/netbox/dcim/models.py +++ b/netbox/dcim/models.py @@ -2414,7 +2414,7 @@ class Cable(ChangeLoggedModel): ) def __str__(self): - return self.label if self.label else '#{}'.format(self.pk) + return self.label or self.id_string def get_absolute_url(self): return reverse('dcim:cable', args=[self.pk]) @@ -2427,6 +2427,15 @@ class Cable(ChangeLoggedModel): super(Cable, self).save(*args, **kwargs) + def __init__(self, *args, **kwargs): + + super(Cable, self).__init__(*args, **kwargs) + + # Create an ID string for use by __str__(). We have to save a copy of pk since it's nullified after .delete() + # is called. + self.id_string = '#{}'.format(self.pk) + + def get_path_endpoints(self): """ Traverse both ends of a cable path and return its connected endpoints. Note that one or both endpoints may be