Skip to content

Filter custom field of type 'selection' for value is null or not-null #11538

@candlerb

Description

@candlerb

NetBox version

v3.4.2

Feature type

Change to existing functionality

Proposed functionality

I would like a way to be able to filter on custom fields of type "selection", to see if they are set (with any value) or unset.

There is an empty filter documented although only for string fields. There are no examples of how to use it, and no test cases for it in netbox/utilities/tests/test_filters.py

Given a custom selection field snmp_module, the following experimentally don't work:

# These return all devices
/api/dcim/devices/?cf_snmp_module__empty
/api/dcim/devices/?cf_snmp_module__empty=

# These always return zero devices
/api/dcim/devices/?cf_snmp_module__empty=true
/api/dcim/devices/?cf_snmp_module__empty=false
/api/dcim/devices/?cf_snmp_module__empty=1
/api/dcim/devices/?cf_snmp_module__empty=0

It does work with regular string fields though (e.g. name__empty=0 and name__empty=1 are fine).

There are other filters which do work on this custom field. The following returns all devices where the custom field is set and the value contains an underscore:

/api/dcim/devices/?cf_snmp_module__ic=_

The following returns all devices where the custom field is non-null, and also does not contain 'z':

/api/dcim/devices/?cf_snmp_module__nic=z

The following returns all devices where the custom field is non-null and does not start with xxxxx (and therefore works as an ugly workaround for what I want):

/api/dcim/devices/?cf_snmp_module__nisw=xxxxx

But the following doesn't work, as it returns all devices (whether or not the field is null):

/api/dcim/devices/?cf_snmp_module__n=xxxxx

What I'm requesting in this feature request is either:

  • the existing empty condition to be extended to mean "null/not null" for selection custom fields; or
  • a new condition, e.g. __null=1 or __null=0

Aside: it's unclear to me whether "empty" was intended to work this way (i.e. this is a bug, not a feature request). I found this code in netbox/netbox/filtersets.py:

        elif isinstance(existing_filter, (
            django_filters.ModelChoiceFilter,
            django_filters.ModelMultipleChoiceFilter,
            TagFilter
        )) or existing_filter.extra.get('choices'):
            # These filter types support only negation
            return FILTER_NEGATION_LOOKUP_MAP

This suggests to me that choices should only support negation (n) - but I've already shown that "ic" and "nic" do work, so maybe these "selection" fields are being considered as strings anyway.

Use case

To be able to filter devices depending on the presence/absence of a value in a selection custom field.

Specifically: I use the custom field snmp_module to choose an appropriate SNMP mobile to scrape by Prometheus. I need to query Netbox to get a list of targets to scrape, and really that should just be exactly those devices where snmp_module has been set.

Currently I have to use a tag to achieve this: I can filter on tag=prom_snmp. But this requires setting both the custom field and a tag on the device.

Database changes

None

External dependencies

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending closureRequires immediate attention to avoid being closed for inactivitystatus: needs ownerThis issue is tentatively accepted pending a volunteer committed to its implementationtype: featureIntroduction of new functionality to the application

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions