diff --git a/netbox/dcim/models.py b/netbox/dcim/models.py index 4014d00e5..8f6e01554 100644 --- a/netbox/dcim/models.py +++ b/netbox/dcim/models.py @@ -68,6 +68,7 @@ class CableTermination(models.Model): cable = models.ForeignKey( to='dcim.Cable', on_delete=models.SET_NULL, + related_name='+', blank=True, null=True ) @@ -2426,12 +2427,6 @@ class Cable(ChangeLoggedModel): super(Cable, self).save(*args, **kwargs) - # Cache the Cable on its two termination points - self.termination_a.cable = self - self.termination_a.save() - self.termination_b.cable = self - self.termination_b.save() - 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 diff --git a/netbox/dcim/signals.py b/netbox/dcim/signals.py index d196b1db6..26e30babb 100644 --- a/netbox/dcim/signals.py +++ b/netbox/dcim/signals.py @@ -23,9 +23,14 @@ def clear_virtualchassis_members(instance, **kwargs): @receiver(post_save, sender=Cable) def update_connected_endpoints(instance, **kwargs): - """ - When a Cable is saved, update its connected endpoints. - """ + + # Cache the Cable on its two termination points + instance.termination_a.cable = instance + instance.termination_a.save() + instance.termination_b.cable = instance + instance.termination_b.save() + + # Check if this Cable has formed a complete path. If so, update both endpoints. endpoint_a, endpoint_b = instance.get_path_endpoints() if endpoint_a is not None and endpoint_b is not None: endpoint_a.connected_endpoint = endpoint_b @@ -36,11 +41,16 @@ def update_connected_endpoints(instance, **kwargs): endpoint_b.save() -@receiver(post_delete, sender=Cable) +@receiver(pre_delete, sender=Cable) def nullify_connected_endpoints(instance, **kwargs): - """ - When a Cable is deleted, nullify its connected endpoints. - """ + + # Disassociate the Cable from its termination points + instance.termination_a.cable = None + instance.termination_a.save() + instance.termination_b.cable = None + instance.termination_b.save() + + # If this Cable was part of a complete path, tear it down endpoint_a, endpoint_b = instance.get_path_endpoints() if endpoint_a is not None and endpoint_b is not None: endpoint_a.connected_endpoint = None