diff --git a/docs/release-notes/version-2.6.md b/docs/release-notes/version-2.6.md index e3ad87ad0..4d81061a5 100644 --- a/docs/release-notes/version-2.6.md +++ b/docs/release-notes/version-2.6.md @@ -8,6 +8,7 @@ * [#3900](https://github.com/netbox-community/netbox/issues/3900) - Fix exception when deleting device types * [#3914](https://github.com/netbox-community/netbox/issues/3914) - Fix interface filter field when unauthenticated +* [#3927](https://github.com/netbox-community/netbox/issues/3927) - Fix exception when deleting devices with secrets assigned --- diff --git a/netbox/secrets/models.py b/netbox/secrets/models.py index 6dcb5abee..e8de8cbd3 100644 --- a/netbox/secrets/models.py +++ b/netbox/secrets/models.py @@ -14,6 +14,7 @@ from django.urls import reverse from django.utils.encoding import force_bytes from taggit.managers import TaggableManager +from dcim.models import Device from extras.models import CustomFieldModel, TaggedItem from utilities.models import ChangeLoggedModel from .exceptions import InvalidKey @@ -359,10 +360,14 @@ class Secret(ChangeLoggedModel, CustomFieldModel): super().__init__(*args, **kwargs) def __str__(self): - if self.role and self.device and self.name: + try: + device = self.device + except Device.DoesNotExist: + device = None + if self.role and device and self.name: return '{} for {} ({})'.format(self.role, self.device, self.name) # Return role and device if no name is set - if self.role and self.device: + if self.role and device: return '{} for {}'.format(self.role, self.device) return 'Secret'