Skip to content

Fixes #3898: Call str of cable on delete to save PK in id_string #3899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/version-2.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* [#3872](https://github.com/netbox-community/netbox/issues/3872) - Paginate related IPs of an address
* [#3876](https://github.com/netbox-community/netbox/issues/3876) - Fixed min/max to ASN input field at the site creation page
* [#3882](https://github.com/netbox-community/netbox/issues/3882) - Fix filtering of devices by rack group
* [#3898](https://github.com/netbox-community/netbox/issues/3898) - Fix deleted message being set to None for cable
* [#3905](https://github.com/netbox-community/netbox/issues/3905) - Fix divide-by-zero on power feeds with low power values

---
Expand Down
16 changes: 9 additions & 7 deletions netbox/dcim/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3027,15 +3027,14 @@ class Meta:
('termination_b_type', 'termination_b_id'),
)

def __str__(self):
if self.label:
return self.label
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# 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)
# A copy of the PK to be used by __str__ in case the object is deleted
self._pk = self.pk

return self.id_string
def __str__(self):
return self.label or '#{}'.format(self._pk)

def get_absolute_url(self):
return reverse('dcim:cable', args=[self.pk])
Expand Down Expand Up @@ -3142,6 +3141,9 @@ def save(self, *args, **kwargs):

super().save(*args, **kwargs)

# Update the private pk used in __str__ in case this is a new object (i.e. just got its pk)
self._pk = self.pk

def to_csv(self):
return (
'{}.{}'.format(self.termination_a_type.app_label, self.termination_a_type.model),
Expand Down
5 changes: 4 additions & 1 deletion netbox/dcim/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,12 @@ def test_cable_creation(self):

def test_cable_deletion(self):
"""
When a Cable is deleted, the `cable` field on its termination points must be nullified.
When a Cable is deleted, the `cable` field on its termination points must be nullified. The str() method
should still return the PK of the string even after being nullified.
"""
self.cable.delete()
self.assertIsNone(self.cable.pk)
self.assertNotEqual(str(self.cable), '#None')
interface1 = Interface.objects.get(pk=self.interface1.pk)
self.assertIsNone(interface1.cable)
interface2 = Interface.objects.get(pk=self.interface2.pk)
Expand Down