Config Context error in Virtual Machine and Device #4249

Closed
opened 2025-12-29 18:34:14 +01:00 by adam · 2 comments
Owner

Originally created by @maximebrun on GitHub (Nov 6, 2020).

Environment

  • Python version: 3.7.9 (from docker image python:3.7-alpine)
  • NetBox version: 2.9.8 (migrate from 2.6.9)

Steps to Reproduce

  1. Create a VM.
  2. Add 2 tags to the VM previously created.
  3. Go to Config Context tab of this VM.

Expected Behavior

Display config context information.

Observed Behavior

An Error message :

MultipleObjectsReturned at /virtualization/virtual-machines/399/config-context/

get() returned more than one VirtualMachine -- it returned 2!

2 is the number of tag attached to the VM. A vm with 4 tags will return a error message "-- it returned 4!".

Comment

Problem exist with virtual machine and device.

I'm not very confident it is the way to really fix the issue but perhaps it can help :

Problem seems to be in ConfigContextModelQuerySet class. Adding a distinct in queryset of class VirtualMachineConfigContextView fix the error (I don't use config context so i can't test more).

 class VirtualMachineConfigContextView(ObjectConfigContextView):
-    queryset = VirtualMachine.objects.annotate_config_context_data()
+    queryset = VirtualMachine.objects.distinct().annotate_config_context_data()
     base_template = 'virtualization/virtualmachine.html'
Originally created by @maximebrun on GitHub (Nov 6, 2020). <!-- NOTE: IF YOUR ISSUE DOES NOT FOLLOW THIS TEMPLATE, IT WILL BE CLOSED. This form is only for reproducible bugs. If you need assistance with NetBox installation, or if you have a general question, DO NOT open an issue. Instead, post to our mailing list: https://groups.google.com/g/netbox-discuss Please describe the environment in which you are running NetBox. Be sure that you are running an unmodified instance of the latest stable release before submitting a bug report, and that any plugins have been disabled. --> ### Environment * Python version: 3.7.9 (from docker image python:3.7-alpine) * NetBox version: 2.9.8 (migrate from 2.6.9) <!-- Describe in detail the exact steps that someone else can take to reproduce this bug using the current stable release of NetBox. Begin with the creation of any necessary database objects and call out every operation being performed explicitly. If reporting a bug in the REST API, be sure to reconstruct the raw HTTP request(s) being made: Don't rely on a client library such as pynetbox. --> ### Steps to Reproduce 1. Create a VM. 2. Add 2 tags to the VM previously created. 3. Go to Config Context tab of this VM. <!-- What did you expect to happen? --> ### Expected Behavior Display config context information. <!-- What happened instead? --> ### Observed Behavior An Error message : ``` MultipleObjectsReturned at /virtualization/virtual-machines/399/config-context/ get() returned more than one VirtualMachine -- it returned 2! ``` 2 is the number of tag attached to the VM. A vm with 4 tags will return a error message "-- it returned 4!". ### Comment Problem exist with virtual machine and device. I'm not very confident it is the way to really fix the issue but perhaps it can help : Problem seems to be in ConfigContextModelQuerySet class. Adding a distinct in queryset of class VirtualMachineConfigContextView fix the error (I don't use config context so i can't test more). ``` class VirtualMachineConfigContextView(ObjectConfigContextView): - queryset = VirtualMachine.objects.annotate_config_context_data() + queryset = VirtualMachine.objects.distinct().annotate_config_context_data() base_template = 'virtualization/virtualmachine.html' ```
adam added the type: bugstatus: accepted labels 2025-12-29 18:34:14 +01:00
adam closed this issue 2025-12-29 18:34:14 +01:00
Author
Owner

@krombel commented on GitHub (Nov 10, 2020):

We are running on v2.9.9 now but still have the issue:

<class 'virtualization.models.VirtualMachine.MultipleObjectsReturned'>

get() returned more than one VirtualMachine -- it returned 2!

Python version: 3.8.6
NetBox version: 2.9.9

This is the stack trace:

netbox_1         | Internal Server Error: /virtualization/virtual-machines/11/config-context/
netbox_1         | Traceback (most recent call last):
netbox_1         |   File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
netbox_1         |     response = get_response(request)
netbox_1         |   File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
netbox_1         |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
netbox_1         |   File "/usr/local/lib/python3.8/site-packages/django/views/generic/base.py", line 73, in view
netbox_1         |     return self.dispatch(request, *args, **kwargs)
netbox_1         |   File "/opt/netbox/netbox/utilities/views.py", line 124, in dispatch
netbox_1         |     return super().dispatch(request, *args, **kwargs)
netbox_1         |   File "/usr/local/lib/python3.8/site-packages/django/views/generic/base.py", line 101, in dispatch
netbox_1         |     return handler(request, *args, **kwargs)
netbox_1         |   File "/opt/netbox/netbox/extras/views.py", line 146, in get
netbox_1         |     obj = get_object_or_404(self.queryset, pk=pk)
netbox_1         |   File "/usr/local/lib/python3.8/site-packages/django/shortcuts.py", line 76, in get_object_or_404
netbox_1         |     return queryset.get(*args, **kwargs)
netbox_1         |   File "/usr/local/lib/python3.8/site-packages/cacheops/query.py", line 353, in get
netbox_1         |     return qs._no_monkey.get(qs, *args, **kwargs)
netbox_1         |   File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 433, in get
netbox_1         |     raise self.model.MultipleObjectsReturned(
netbox_1         | virtualization.models.VirtualMachine.MultipleObjectsReturned: get() returned more than one VirtualMachine -- it returned 2!
netbox_1         | 192.168.0.2 - - [10/Nov/2020:13:58:48 +0000] "GET /virtualization/virtual-machines/11/config-context/ HTTP/1.0" 500 1854 "https://nb.ov.ffmuc.net/virtualization/virtual-machines/11/" "Mozilla/5.0"

If it makes any difference: We are running netbox in docker. We use the tags to add or remove config context based on tags.
In case we remove one tag we can see the config context again in the UI. The API does not suffer of this issue so this is only an UI issue

@krombel commented on GitHub (Nov 10, 2020): We are running on v2.9.9 now but still have the issue: ``` <class 'virtualization.models.VirtualMachine.MultipleObjectsReturned'> get() returned more than one VirtualMachine -- it returned 2! Python version: 3.8.6 NetBox version: 2.9.9 ``` This is the stack trace: ``` netbox_1 | Internal Server Error: /virtualization/virtual-machines/11/config-context/ netbox_1 | Traceback (most recent call last): netbox_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner netbox_1 | response = get_response(request) netbox_1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response netbox_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs) netbox_1 | File "/usr/local/lib/python3.8/site-packages/django/views/generic/base.py", line 73, in view netbox_1 | return self.dispatch(request, *args, **kwargs) netbox_1 | File "/opt/netbox/netbox/utilities/views.py", line 124, in dispatch netbox_1 | return super().dispatch(request, *args, **kwargs) netbox_1 | File "/usr/local/lib/python3.8/site-packages/django/views/generic/base.py", line 101, in dispatch netbox_1 | return handler(request, *args, **kwargs) netbox_1 | File "/opt/netbox/netbox/extras/views.py", line 146, in get netbox_1 | obj = get_object_or_404(self.queryset, pk=pk) netbox_1 | File "/usr/local/lib/python3.8/site-packages/django/shortcuts.py", line 76, in get_object_or_404 netbox_1 | return queryset.get(*args, **kwargs) netbox_1 | File "/usr/local/lib/python3.8/site-packages/cacheops/query.py", line 353, in get netbox_1 | return qs._no_monkey.get(qs, *args, **kwargs) netbox_1 | File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 433, in get netbox_1 | raise self.model.MultipleObjectsReturned( netbox_1 | virtualization.models.VirtualMachine.MultipleObjectsReturned: get() returned more than one VirtualMachine -- it returned 2! netbox_1 | 192.168.0.2 - - [10/Nov/2020:13:58:48 +0000] "GET /virtualization/virtual-machines/11/config-context/ HTTP/1.0" 500 1854 "https://nb.ov.ffmuc.net/virtualization/virtual-machines/11/" "Mozilla/5.0" ``` If it makes any difference: We are running netbox in docker. We use the tags to add or remove config context based on tags. In case we remove one tag we can see the config context again in the UI. The API does not suffer of this issue so this is only an UI issue
Author
Owner

@colawebrunner commented on GitHub (Nov 11, 2020):

We are also now running on v2.9.9, but still having the issue:

<class 'virtualization.models.VirtualMachine.MultipleObjectsReturned'>

get() returned more than one VirtualMachine -- it returned 2!

Python version: 3.7.9
NetBox version: 2.9.9

This is the traceback:

Traceback (most recent call last):
  File "/opt/netbox/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/opt/netbox/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/netbox/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 73, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/netbox/netbox/utilities/views.py", line 124, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/opt/netbox/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 101, in dispatch
    return handler(request, *args, **kwargs)
  File "/opt/netbox/netbox/extras/views.py", line 146, in get
    obj = get_object_or_404(self.queryset, pk=pk)
  File "/opt/netbox/venv/lib/python3.7/site-packages/django/shortcuts.py", line 76, in get_object_or_404
    return queryset.get(*args, **kwargs)
  File "/opt/netbox/venv/lib/python3.7/site-packages/cacheops/query.py", line 353, in get
    return qs._no_monkey.get(qs, *args, **kwargs)
  File "/opt/netbox/venv/lib/python3.7/site-packages/django/db/models/query.py", line 436, in get
    num if not limit or num < limit else 'more than %s' % (limit - 1),

Exception Type: MultipleObjectsReturned at /virtualization/virtual-machines/1974/config-context/
Exception Value: get() returned more than one VirtualMachine -- it returned 2!
@colawebrunner commented on GitHub (Nov 11, 2020): We are also now running on v2.9.9, but still having the issue: ``` <class 'virtualization.models.VirtualMachine.MultipleObjectsReturned'> get() returned more than one VirtualMachine -- it returned 2! Python version: 3.7.9 NetBox version: 2.9.9 ``` This is the traceback: ``` Traceback (most recent call last): File "/opt/netbox/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/opt/netbox/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 179, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/opt/netbox/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 73, in view return self.dispatch(request, *args, **kwargs) File "/opt/netbox/netbox/utilities/views.py", line 124, in dispatch return super().dispatch(request, *args, **kwargs) File "/opt/netbox/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 101, in dispatch return handler(request, *args, **kwargs) File "/opt/netbox/netbox/extras/views.py", line 146, in get obj = get_object_or_404(self.queryset, pk=pk) File "/opt/netbox/venv/lib/python3.7/site-packages/django/shortcuts.py", line 76, in get_object_or_404 return queryset.get(*args, **kwargs) File "/opt/netbox/venv/lib/python3.7/site-packages/cacheops/query.py", line 353, in get return qs._no_monkey.get(qs, *args, **kwargs) File "/opt/netbox/venv/lib/python3.7/site-packages/django/db/models/query.py", line 436, in get num if not limit or num < limit else 'more than %s' % (limit - 1), Exception Type: MultipleObjectsReturned at /virtualization/virtual-machines/1974/config-context/ Exception Value: get() returned more than one VirtualMachine -- it returned 2! ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4249