Просмотр исходного кода

Fixes #3927: Fix exception when deleting devices with secrets assigned

Jeremy Stretch 6 лет назад
Родитель
Сommit
dda9a2ee1c
2 измененных файлов с 8 добавлено и 2 удалено
  1. 1 0
      docs/release-notes/version-2.6.md
  2. 7 2
      netbox/secrets/models.py

+ 1 - 0
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
 
 ---
 

+ 7 - 2
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'