Enable API filtering of foreign key properties by null value #8692

Closed
opened 2025-12-29 20:39:58 +01:00 by adam · 6 comments
Owner

Originally created by @decoupca on GitHub (Sep 28, 2023).

NetBox version

v3.6.3

Feature type

New functionality

Proposed functionality

Currently, properties of foreign key relationships can only be filtered by negation. As far as I can tell, there is no way to filter foreign key properties based on whether or not a value exists.

This FR would introduce the ability to filter foreign key properties by null value using boolean logic, exactly how numeric and string properties can be filtered with the empty operator.

Use case

Many use cases may benefit from looking up objects with properties that are currently null.

For example, I want to find a list of all interfaces that are connected. If this feature were implemented, I could construct a query like this: /api/dcim/interfaces/?connected_endpoints__empty=false

Database changes

None

External dependencies

None

Originally created by @decoupca on GitHub (Sep 28, 2023). ### NetBox version v3.6.3 ### Feature type New functionality ### Proposed functionality Currently, properties of foreign key relationships can only be [filtered by negation](https://docs.netbox.dev/en/stable/reference/filtering/#foreign-keys-other-fields). As far as I can tell, there is no way to filter foreign key properties based on whether or not a value exists. This FR would introduce the ability to filter foreign key properties by null value using boolean logic, exactly how numeric and string properties [can be filtered with the `empty` operator](https://docs.netbox.dev/en/stable/reference/filtering/#string-fields). ### Use case Many use cases may benefit from looking up objects with properties that are currently null. For example, I want to find a list of all interfaces that are connected. If this feature were implemented, I could construct a query like this: `/api/dcim/interfaces/?connected_endpoints__empty=false` ### Database changes None ### External dependencies None
adam added the type: featurepending closure labels 2025-12-29 20:39:59 +01:00
adam closed this issue 2025-12-29 20:39:59 +01:00
Author
Owner

@miaow2 commented on GitHub (Sep 29, 2023):

Hi @decoupca, you can filter foreign key properties based on whether or not a value exists

for example:
filter devices with assigned tenants
https://demo.netbox.dev/dcim/devices/?tenant_id__n=null

filter devices without tenants
https://demo.netbox.dev/dcim/devices/?tenant_id=null

@miaow2 commented on GitHub (Sep 29, 2023): Hi @decoupca, you can filter foreign key properties based on whether or not a value exists for example: filter devices with assigned tenants https://demo.netbox.dev/dcim/devices/?tenant_id__n=null filter devices without tenants https://demo.netbox.dev/dcim/devices/?tenant_id=null
Author
Owner

@decoupca commented on GitHub (Sep 29, 2023):

@miaow2 Correct. You can filter foreign key properties based on null value. I didn't use the right terms in the FR. The core issue I face is that I can't do the same for a property like connected_endpoints. I'm not sure what type of property connected_endpoints is, but it's not filterable by null value.

These don't work:

  • /api/dcim/interfaces/?connected_endpoints__n=null
  • /api/dcim/interfaces/?connected_endpoints__empty=false
@decoupca commented on GitHub (Sep 29, 2023): @miaow2 Correct. You can filter foreign key properties based on null value. I didn't use the right terms in the FR. The core issue I face is that I can't do the same for a property like `connected_endpoints`. I'm not sure what type of property `connected_endpoints` is, but it's not filterable by null value. These don't work: * `/api/dcim/interfaces/?connected_endpoints__n=null` * `/api/dcim/interfaces/?connected_endpoints__empty=false`
Author
Owner

@miaow2 commented on GitHub (Sep 29, 2023):

@decoupca you can't filter property connected_endpoints because there is no filter on this property

you can try
/api/dcim/interfaces/?cabled=true or /api/dcim/interfaces/?cabled=false
or
/api/dcim/interfaces/?connected=true or /api/dcim/interfaces/?cabled=false

check swagger for filters https://demo.netbox.dev/api/schema/swagger-ui/#/dcim/dcim_interfaces_list

@miaow2 commented on GitHub (Sep 29, 2023): @decoupca you can't filter property `connected_endpoints` because there is no filter on this property you can try `/api/dcim/interfaces/?cabled=true` or `/api/dcim/interfaces/?cabled=false` or `/api/dcim/interfaces/?connected=true` or `/api/dcim/interfaces/?cabled=false` check swagger for filters https://demo.netbox.dev/api/schema/swagger-ui/#/dcim/dcim_interfaces_list
Author
Owner

@decoupca commented on GitHub (Sep 29, 2023):

@miaow2 good info-- thanks. That does solve my use case.

However, there are other fields that can't be filtered by null value or another property (as with connected_endpooints and cabled). For example, these don't work:

  • /api/dcim/interfaces/?poe_mode__n=null
  • /api/dcim/interfaces/?mtu__n=null

I can imagine several cases when it would be useful to find interfaces without any value assigned to these (or other) properties.

@decoupca commented on GitHub (Sep 29, 2023): @miaow2 good info-- thanks. That does solve my use case. However, there are other fields that can't be filtered by null value or another property (as with `connected_endpooints` and `cabled`). For example, these don't work: * `/api/dcim/interfaces/?poe_mode__n=null` * `/api/dcim/interfaces/?mtu__n=null` I can imagine several cases when it would be useful to find interfaces without any value assigned to these (or other) properties.
Author
Owner

@github-actions[bot] commented on GitHub (Dec 29, 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 (Dec 29, 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).
Author
Owner

@github-actions[bot] commented on GitHub (Jan 28, 2024):

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.

@github-actions[bot] commented on GitHub (Jan 28, 2024): 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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#8692