Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
abhi1693 opened this issue Apr 27, 2023 · 1 comment · Fixed by #12368
Closed
Assignees
Labels
type: bug A confirmed report of unexpected behavior in the application

Comments

@abhi1693
Copy link
Member

abhi1693 commented Apr 27, 2023

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.
@abhi1693 abhi1693 added the type: bug A confirmed report of unexpected behavior in the application label Apr 27, 2023
@kkthxbye-code
Copy link
Contributor

kkthxbye-code commented Apr 27, 2023

Just to add some context, it's caused #11550 which was merged in 3.4.8, so I would guess this doesn't only effect 3.5.

abhi1693 added a commit that referenced this issue Apr 27, 2023
@abhi1693 abhi1693 self-assigned this Apr 27, 2023
jeremystretch added a commit that referenced this issue May 2, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants