DoesNotExist exception triggered when deleting a device with one or more secrets #3165

Closed
opened 2025-12-29 18:26:15 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Jan 15, 2020).

Originally assigned to: @jeremystretch on GitHub.

Environment

  • Python version: 2.6.12
  • NetBox version: 3.6.8

Steps to Reproduce

  1. Create a device
  2. Assign one or more secrets to the device
  3. Delete the device

Expected Behavior

The device and its associated secrets should be deleted, and the change log should be updated to reflect the deletion of each object.

Observed Behavior

The device and secrets are deleted, however an exception is raised while attempting to record the secrets deletion in the change log:

Internal Server Error: /dcim/devices/1611/delete/
Traceback (most recent call last):
  File "/home/jstretch/.virtualenvs/netbox/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 164, in __get__
    rel_obj = self.field.get_cached_value(instance)
  File "/home/jstretch/.virtualenvs/netbox/lib/python3.6/site-packages/django/db/models/fields/mixins.py", line 13, in get_cached_value
    return instance._state.fields_cache[cache_name]
KeyError: 'device'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/jstretch/.virtualenvs/netbox/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/jstretch/netbox/netbox/extras/middleware.py", line 110, in __call__
    objectchange = instance.to_objectchange(action)
  File "/home/jstretch/netbox/netbox/utilities/models.py", line 33, in to_objectchange
    object_repr=str(self),
  File "/home/jstretch/netbox/netbox/secrets/models.py", line 362, in __str__
    if self.role and self.device and self.name:
  File "/home/jstretch/.virtualenvs/netbox/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 178, in __get__
    rel_obj = self.get_object(instance)
  File "/home/jstretch/.virtualenvs/netbox/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 145, in get_object
    return qs.get(self.field.get_reverse_related_filter(instance))
  File "/home/jstretch/.virtualenvs/netbox/lib/python3.6/site-packages/cacheops/query.py", line 356, in get
    return qs._no_monkey.get(qs, *args, **kwargs)
  File "/home/jstretch/.virtualenvs/netbox/lib/python3.6/site-packages/django/db/models/query.py", line 408, in get
    self.model._meta.object_name
dcim.models.Device.DoesNotExist: Device matching query does not exist.

This is due to the __str__() method on Secret attempting to resolve the parent Device, which no longer exists at the point of execution.

Originally created by @jeremystretch on GitHub (Jan 15, 2020). Originally assigned to: @jeremystretch on GitHub. ### Environment * Python version: 2.6.12 * NetBox version: 3.6.8 ### Steps to Reproduce 1. Create a device 2. Assign one or more secrets to the device 3. Delete the device ### Expected Behavior The device and its associated secrets should be deleted, and the change log should be updated to reflect the deletion of each object. ### Observed Behavior The device and secrets are deleted, however an exception is raised while attempting to record the secrets deletion in the change log: ``` Internal Server Error: /dcim/devices/1611/delete/ Traceback (most recent call last): File "/home/jstretch/.virtualenvs/netbox/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 164, in __get__ rel_obj = self.field.get_cached_value(instance) File "/home/jstretch/.virtualenvs/netbox/lib/python3.6/site-packages/django/db/models/fields/mixins.py", line 13, in get_cached_value return instance._state.fields_cache[cache_name] KeyError: 'device' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/jstretch/.virtualenvs/netbox/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/home/jstretch/netbox/netbox/extras/middleware.py", line 110, in __call__ objectchange = instance.to_objectchange(action) File "/home/jstretch/netbox/netbox/utilities/models.py", line 33, in to_objectchange object_repr=str(self), File "/home/jstretch/netbox/netbox/secrets/models.py", line 362, in __str__ if self.role and self.device and self.name: File "/home/jstretch/.virtualenvs/netbox/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 178, in __get__ rel_obj = self.get_object(instance) File "/home/jstretch/.virtualenvs/netbox/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 145, in get_object return qs.get(self.field.get_reverse_related_filter(instance)) File "/home/jstretch/.virtualenvs/netbox/lib/python3.6/site-packages/cacheops/query.py", line 356, in get return qs._no_monkey.get(qs, *args, **kwargs) File "/home/jstretch/.virtualenvs/netbox/lib/python3.6/site-packages/django/db/models/query.py", line 408, in get self.model._meta.object_name dcim.models.Device.DoesNotExist: Device matching query does not exist. ``` This is due to the `__str__()` method on Secret attempting to resolve the parent Device, which no longer exists at the point of execution.
adam added the type: bugstatus: accepted labels 2025-12-29 18:26:15 +01:00
adam closed this issue 2025-12-29 18:26:16 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3165