From 3d78a673431bfe370e95a4f82a90a11e0041932d Mon Sep 17 00:00:00 2001 From: Saria Hajjar Date: Mon, 13 Jan 2020 14:57:21 +0000 Subject: [PATCH] Store a private copy of the pk during init and use that with __str__ --- netbox/dcim/models.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/netbox/dcim/models.py b/netbox/dcim/models.py index 89ff8bc15..d7c85ff00 100644 --- a/netbox/dcim/models.py +++ b/netbox/dcim/models.py @@ -3027,24 +3027,18 @@ class Cable(ChangeLoggedModel): ('termination_b_type', 'termination_b_id'), ) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # A copy of the PK to be used by __str__ in case the object is deleted + self._pk = self.pk + def __str__(self): - if self.label: - return self.label - - # Save a copy of the PK on the instance since it's nullified if .delete() is called - if not hasattr(self, 'id_string'): - self.id_string = '#{}'.format(self.pk) - - return self.id_string + return self.label or '#{}'.format(self._pk) def get_absolute_url(self): return reverse('dcim:cable', args=[self.pk]) - def delete(self, *args, **kwargs): - # Trigger the __str__ method to save the pk into `self.id_string` - str(self) - super().delete(*args, **kwargs) - def clean(self): # Validate that termination A exists