Exception with multiselect custom field and query="null" #8535

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

Originally created by @candlerb on GitHub (Aug 30, 2023).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.5.9

Python version

3.8

Steps to Reproduce

  1. Create a custom field of type "multiple selection". Let's say it's called "foo" and is applied to dcim.device, and has allowed values "a,b,c"
  2. Query /dcim/devices/?cf_foo=null

Expected Behavior

The request to be processed, or at least ignored. (Ideally it would show devices which have no selection made for the given custom field)

Observed Behavior

A ValueError was raised.

image

Here is the backtrace:

Internal Server Error: /dcim/devices/

ValueError at /dcim/devices/
Cannot use None as a query value

Request Method: GET
Request URL: https://netbox.example.net/dcim/devices/?cf_foo=null
Django Version: 4.1.10
Python Executable: /opt/netbox/venv/bin/python3.8
Python Version: 3.8.10
Python Path: ['/opt/netbox/netbox', '/opt/netbox', '/opt/netbox/venv/bin', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/opt/netbox/venv/lib/python3.8/site-packages']
Server time: Wed, 30 Aug 2023 09:43:17 +0000
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'corsheaders',
 'debug_toolbar',
 'graphiql_debug_toolbar',
 'django_filters',
 'django_tables2',
 'django_prometheus',
 'graphene_django',
 'mptt',
 'rest_framework',
 'social_django',
 'taggit',
 'timezone_field',
 'core',
 'circuits',
 'dcim',
 'ipam',
 'extras',
 'tenancy',
 'users',
 'utilities',
 'virtualization',
 'wireless',
 'django_rq',
 'drf_spectacular',
 'drf_spectacular_sidecar',
 'netbox_prometheus_sd.PrometheusSD',
 'netbox_topology_views.TopologyViewsConfig']
Installed Middleware:
['graphiql_debug_toolbar.middleware.DebugToolbarMiddleware',
 'django_prometheus.middleware.PrometheusBeforeMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'netbox.middleware.RemoteUserMiddleware',
 'netbox.middleware.CoreMiddleware',
 'netbox.middleware.MaintenanceModeMiddleware',
 'django_prometheus.middleware.PrometheusAfterMiddleware']


Traceback (most recent call last):
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 56, in inner
    response = get_response(request)
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/netbox/netbox/netbox/views/generic/base.py", line 77, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/opt/netbox/netbox/utilities/views.py", line 99, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 142, in dispatch
    return handler(request, *args, **kwargs)
  File "/opt/netbox/netbox/netbox/views/generic/bulk_views.py", line 130, in get
    self.queryset = self.filterset(request.GET, self.queryset, request=request).qs
  File "/opt/netbox/venv/lib/python3.8/site-packages/django_filters/filterset.py", line 230, in qs
    qs = self.filter_queryset(qs)
  File "/opt/netbox/venv/lib/python3.8/site-packages/django_filters/filterset.py", line 213, in filter_queryset
    queryset = self.filters[name].filter(queryset, value)
  File "/opt/netbox/venv/lib/python3.8/site-packages/django_filters/filters.py", line 263, in filter
    qs = self.get_method(qs)(q)
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1421, in filter
    return self._filter_or_exclude(False, args, kwargs)
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1439, in _filter_or_exclude
    clone._filter_or_exclude_inplace(negate, args, kwargs)
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1446, in _filter_or_exclude_inplace
    self._query.add_q(Q(*args, **kwargs))
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1532, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1562, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1388, in build_filter
    return self._add_q(
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1562, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1478, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1308, in build_lookup
    raise ValueError("Cannot use None as a query value")

Exception Type: ValueError at /dcim/devices/
Exception Value: Cannot use None as a query value
Raised during: dcim.views.DeviceListView
Request information:
USER: brian

GET:
cf_foo = 'null'

POST: No POST data

FILES: No FILES data

Additional info:

  • The queries cf_foo=null and cf_foo__n=null work with custom fields of type "select" rather than "multiselect"
  • With multiselect, the query cf_foo=a works as expected
  • With multiselect, the query cf_foo__n=a does not work (it has no effect, all devices are shown); similarly, cf_foo__n=null has no effect.
  • Relates to https://github.com/netbox-community/netbox/issues/11538#issuecomment-1635839720
Originally created by @candlerb on GitHub (Aug 30, 2023). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.5.9 ### Python version 3.8 ### Steps to Reproduce 1. Create a custom field of type "multiple selection". Let's say it's called "foo" and is applied to dcim.device, and has allowed values "a,b,c" 2. Query `/dcim/devices/?cf_foo=null` ### Expected Behavior The request to be processed, or at least ignored. (Ideally it would show devices which have no selection made for the given custom field) ### Observed Behavior A ValueError was raised. ![image](https://github.com/netbox-community/netbox/assets/44789/a86dc86c-27d2-4edf-a17f-12294fdcd659) Here is the backtrace: ``` Internal Server Error: /dcim/devices/ ValueError at /dcim/devices/ Cannot use None as a query value Request Method: GET Request URL: https://netbox.example.net/dcim/devices/?cf_foo=null Django Version: 4.1.10 Python Executable: /opt/netbox/venv/bin/python3.8 Python Version: 3.8.10 Python Path: ['/opt/netbox/netbox', '/opt/netbox', '/opt/netbox/venv/bin', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/opt/netbox/venv/lib/python3.8/site-packages'] Server time: Wed, 30 Aug 2023 09:43:17 +0000 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.humanize', 'corsheaders', 'debug_toolbar', 'graphiql_debug_toolbar', 'django_filters', 'django_tables2', 'django_prometheus', 'graphene_django', 'mptt', 'rest_framework', 'social_django', 'taggit', 'timezone_field', 'core', 'circuits', 'dcim', 'ipam', 'extras', 'tenancy', 'users', 'utilities', 'virtualization', 'wireless', 'django_rq', 'drf_spectacular', 'drf_spectacular_sidecar', 'netbox_prometheus_sd.PrometheusSD', 'netbox_topology_views.TopologyViewsConfig'] Installed Middleware: ['graphiql_debug_toolbar.middleware.DebugToolbarMiddleware', 'django_prometheus.middleware.PrometheusBeforeMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.security.SecurityMiddleware', 'netbox.middleware.RemoteUserMiddleware', 'netbox.middleware.CoreMiddleware', 'netbox.middleware.MaintenanceModeMiddleware', 'django_prometheus.middleware.PrometheusAfterMiddleware'] Traceback (most recent call last): File "/opt/netbox/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 56, in inner response = get_response(request) File "/opt/netbox/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/opt/netbox/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 103, in view return self.dispatch(request, *args, **kwargs) File "/opt/netbox/netbox/netbox/views/generic/base.py", line 77, in dispatch return super().dispatch(request, *args, **kwargs) File "/opt/netbox/netbox/utilities/views.py", line 99, in dispatch return super().dispatch(request, *args, **kwargs) File "/opt/netbox/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 142, in dispatch return handler(request, *args, **kwargs) File "/opt/netbox/netbox/netbox/views/generic/bulk_views.py", line 130, in get self.queryset = self.filterset(request.GET, self.queryset, request=request).qs File "/opt/netbox/venv/lib/python3.8/site-packages/django_filters/filterset.py", line 230, in qs qs = self.filter_queryset(qs) File "/opt/netbox/venv/lib/python3.8/site-packages/django_filters/filterset.py", line 213, in filter_queryset queryset = self.filters[name].filter(queryset, value) File "/opt/netbox/venv/lib/python3.8/site-packages/django_filters/filters.py", line 263, in filter qs = self.get_method(qs)(q) File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1421, in filter return self._filter_or_exclude(False, args, kwargs) File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1439, in _filter_or_exclude clone._filter_or_exclude_inplace(negate, args, kwargs) File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1446, in _filter_or_exclude_inplace self._query.add_q(Q(*args, **kwargs)) File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1532, in add_q clause, _ = self._add_q(q_object, self.used_aliases) File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1562, in _add_q child_clause, needed_inner = self.build_filter( File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1388, in build_filter return self._add_q( File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1562, in _add_q child_clause, needed_inner = self.build_filter( File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1478, in build_filter condition = self.build_lookup(lookups, col, value) File "/opt/netbox/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1308, in build_lookup raise ValueError("Cannot use None as a query value") Exception Type: ValueError at /dcim/devices/ Exception Value: Cannot use None as a query value Raised during: dcim.views.DeviceListView Request information: USER: brian GET: cf_foo = 'null' POST: No POST data FILES: No FILES data ``` Additional info: * The queries `cf_foo=null` and `cf_foo__n=null` work with custom fields of type "select" rather than "multiselect" * With multiselect, the query `cf_foo=a` works as expected * With multiselect, the query `cf_foo__n=a` does not work (it has no effect, all devices are shown); similarly, `cf_foo__n=null` has no effect. * Relates to https://github.com/netbox-community/netbox/issues/11538#issuecomment-1635839720
adam added the type: bugstatus: acceptedseverity: low labels 2025-12-29 20:37:51 +01:00
adam closed this issue 2025-12-29 20:37:51 +01:00
Author
Owner

@github-actions[bot] commented on GitHub (Nov 30, 2023):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

@github-actions[bot] commented on GitHub (Nov 30, 2023): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. **Do not** attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#8535