-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Filter custom field of type 'selection' for value is null or not-null #11538
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
Comments
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. |
I should add, to find devices with custom field unset I also tried
but that returns all devices. In any case, what I need is the opposite: "all devices where |
#11539 may be related to this, though I don't believe it's blocking. |
This request is to add the |
By chance I came across this in
It looks like there's a magic string value here, and experimentally, the following filters do appear to work:
The latter one is exactly what I was looking for (custom field value is not null). I would therefore be happy to close this issue, except that as far as I can tell, this functionality is completely undocumented and therefore I'm loathe to depend on it. Could it be documented here? And could it be made clear what the intended difference is between I observe that there are just two tests using |
Unfortunately, now I need this custom field to be
|
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. |
This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary. |
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 innetbox/utilities/tests/test_filters.py
Given a custom selection field
snmp_module
, the following experimentally don't work:It does work with regular string fields though (e.g.
name__empty=0
andname__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:
The following returns all devices where the custom field is non-null, and also does not contain '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):But the following doesn't work, as it returns all devices (whether or not the field is null):
What I'm requesting in this feature request is either:
empty
condition to be extended to mean "null/not null" for selection custom fields; or__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
: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 wheresnmp_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
The text was updated successfully, but these errors were encountered: