RelatedObjectDoesNotExist is raised when the form contains options to set multiple objects #7953

Closed
opened 2025-12-29 20:30:26 +01:00 by adam · 1 comment
Owner

Originally created by @abhi1693 on GitHub (Apr 27, 2023).

Originally assigned to: @abhi1693 on GitHub.

NetBox version

v3.5.0-beta2

Python version

3.10

Steps to Reproduce

This is found via a plugin for a form written at https://github.com/Onemind-Services-LLC/netbox-device-lifecycle-mgmt/blob/dev/netbox_device_lifecycle_mgmt/forms/model_forms.py#L31

When a user selected device_type and module_type fields together, the page crashes

Expected Behavior

Page should not crash

When I apply the following patch, the page works as expected

diff --git a/netbox/netbox/models/__init__.py b/netbox/netbox/models/__init__.py
index fe25bb837..c0f679e4f 100644
--- a/netbox/netbox/models/__init__.py
+++ b/netbox/netbox/models/__init__.py
@@ -67,8 +67,8 @@ class NetBoxModel(CloningMixin, NetBoxFeatureSet, models.Model):
 
         for field in self._meta.get_fields():
             if isinstance(field, GenericForeignKey):
-                ct_value = getattr(self, field.ct_field)
-                fk_value = getattr(self, field.fk_field)
+                ct_value = getattr(self, field.ct_field, None)
+                fk_value = getattr(self, field.fk_field, None)
 
                 if ct_value is None and fk_value is not None:
                     raise ValidationError({

Observed Behavior

Internal Server Error: /plugins/device-lcm/hardware-notices/add/
Traceback (most recent call last):
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 56, in inner
    response = get_response(request)
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/asaharan/PycharmProjects/netbox/netbox/netbox/views/generic/object_views.py", line 167, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/home/asaharan/PycharmProjects/netbox/netbox/netbox/views/generic/base.py", line 26, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/home/asaharan/PycharmProjects/netbox/netbox/utilities/views.py", line 99, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 142, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/asaharan/PycharmProjects/netbox/netbox/netbox/views/generic/object_views.py", line 255, in post
    if form.is_valid():
  File "/home/asaharan/PycharmProjects/netbox/netbox/utilities/forms/mixins.py", line 52, in is_valid
    is_valid = super().is_valid()
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/forms/forms.py", line 205, in is_valid
    return self.is_bound and not self.errors
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/forms/forms.py", line 200, in errors
    self.full_clean()
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/forms/forms.py", line 439, in full_clean
    self._post_clean()
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/forms/models.py", line 492, in _post_clean
    self.instance.full_clean(exclude=exclude, validate_unique=False)
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/db/models/base.py", line 1455, in full_clean
    self.clean()
  File "/home/asaharan/PycharmProjects/netbox/netbox/netbox/models/__init__.py", line 71, in clean
    ct_value = getattr(self, field.ct_field)
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/db/models/fields/related_descriptors.py", line 217, in __get__
    raise self.RelatedObjectDoesNotExist(
netbox_device_lifecycle_mgmt.models.hardware.HardwareNotice.object_type.RelatedObjectDoesNotExist: HardwareNotice has no object_type.
Originally created by @abhi1693 on GitHub (Apr 27, 2023). Originally assigned to: @abhi1693 on GitHub. ### NetBox version v3.5.0-beta2 ### Python version 3.10 ### Steps to Reproduce This is found via a plugin for a form written at https://github.com/Onemind-Services-LLC/netbox-device-lifecycle-mgmt/blob/dev/netbox_device_lifecycle_mgmt/forms/model_forms.py#L31 When a user selected `device_type` and `module_type` fields together, the page crashes ### Expected Behavior Page should not crash When I apply the following patch, the page works as expected ``` diff --git a/netbox/netbox/models/__init__.py b/netbox/netbox/models/__init__.py index fe25bb837..c0f679e4f 100644 --- a/netbox/netbox/models/__init__.py +++ b/netbox/netbox/models/__init__.py @@ -67,8 +67,8 @@ class NetBoxModel(CloningMixin, NetBoxFeatureSet, models.Model): for field in self._meta.get_fields(): if isinstance(field, GenericForeignKey): - ct_value = getattr(self, field.ct_field) - fk_value = getattr(self, field.fk_field) + ct_value = getattr(self, field.ct_field, None) + fk_value = getattr(self, field.fk_field, None) if ct_value is None and fk_value is not None: raise ValidationError({ ``` ### Observed Behavior ``` Internal Server Error: /plugins/device-lcm/hardware-notices/add/ Traceback (most recent call last): File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 56, in inner response = get_response(request) File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 103, in view return self.dispatch(request, *args, **kwargs) File "/home/asaharan/PycharmProjects/netbox/netbox/netbox/views/generic/object_views.py", line 167, in dispatch return super().dispatch(request, *args, **kwargs) File "/home/asaharan/PycharmProjects/netbox/netbox/netbox/views/generic/base.py", line 26, in dispatch return super().dispatch(request, *args, **kwargs) File "/home/asaharan/PycharmProjects/netbox/netbox/utilities/views.py", line 99, in dispatch return super().dispatch(request, *args, **kwargs) File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 142, in dispatch return handler(request, *args, **kwargs) File "/home/asaharan/PycharmProjects/netbox/netbox/netbox/views/generic/object_views.py", line 255, in post if form.is_valid(): File "/home/asaharan/PycharmProjects/netbox/netbox/utilities/forms/mixins.py", line 52, in is_valid is_valid = super().is_valid() File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/forms/forms.py", line 205, in is_valid return self.is_bound and not self.errors File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/forms/forms.py", line 200, in errors self.full_clean() File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/forms/forms.py", line 439, in full_clean self._post_clean() File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/forms/models.py", line 492, in _post_clean self.instance.full_clean(exclude=exclude, validate_unique=False) File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/db/models/base.py", line 1455, in full_clean self.clean() File "/home/asaharan/PycharmProjects/netbox/netbox/netbox/models/__init__.py", line 71, in clean ct_value = getattr(self, field.ct_field) File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/db/models/fields/related_descriptors.py", line 217, in __get__ raise self.RelatedObjectDoesNotExist( netbox_device_lifecycle_mgmt.models.hardware.HardwareNotice.object_type.RelatedObjectDoesNotExist: HardwareNotice has no object_type. ```
adam added the type: bug label 2025-12-29 20:30:26 +01:00
adam closed this issue 2025-12-29 20:30:26 +01:00
Author
Owner

@kkthxbye-code commented on GitHub (Apr 27, 2023):

Just to add some context, it's caused https://github.com/netbox-community/netbox/pull/11550 which was merged in 3.4.8, so I would guess this doesn't only effect 3.5.

@kkthxbye-code commented on GitHub (Apr 27, 2023): Just to add some context, it's caused https://github.com/netbox-community/netbox/pull/11550 which was merged in 3.4.8, so I would guess this doesn't only effect 3.5.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7953