Enable filtering of ObjectVar selections in custom scripts #3968

Closed
opened 2025-12-29 18:32:22 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Aug 11, 2020).

Originally assigned to: @jeremystretch on GitHub.

Environment

  • Python version: 3.6.9
  • NetBox version: 2.8.9

Proposed Functionality

Enable a custom script author to arbitrarily restrict the results presented by an ObjectVar (or MultiObjectVar) field. In the past, this was done by specifying a custom queryset calling filter() and exlclude() as necessary, but #4255 updated the form fields to populate results by making independent REST API requests. This allowed for much better performance when dealing with large sets of objects at the cost of flexibility.

One current partial workaround is to use NetBox's internal APISelect widget, however this component is not part of any public API offered by NetBox and is therefore not supported. Another workaround is to create a slightly customized form field which uses a direct ORM call to pre-populate the selection list (as opposed to a REST API call), however as discussed in #4255 this imposes sometimes severe performance penalties. This issue seeks to identify and implement a proper, documented solution.

Although a precise solution remains to be identified, the overall goal is to replicate the functionality currently provided by the APISelect widget in a way that's less verbose and more convenient for users. Mechanisms are needed to provide control over the following:

  • The string used for displaying the object in the selection list
  • The value used to represent the object within the form data (e.g. a primary key or slug)
  • How to determine whether a list option is disabled
  • Additional query parameters used to arbitrarily restrict the list of objects
  • Applying a query parameter based on the value of another field within the form (field chaining)

Use Case

When writing a custom script in which the user is prompted to select one or more NetBox objects, it may be desirable to limit the choices presented to the user. For example, when presenting the user with a list of available sites, the custom script author may want to restrict the list to show only sites in the "active" status. This is not currently possible using ObjectVar as described in the documentation.

Database Changes

None

External Dependencies

None anticipated

Originally created by @jeremystretch on GitHub (Aug 11, 2020). Originally assigned to: @jeremystretch on GitHub. ### Environment * Python version: 3.6.9 * NetBox version: 2.8.9 ### Proposed Functionality Enable a custom script author to arbitrarily restrict the results presented by an ObjectVar (or MultiObjectVar) field. In the past, this was done by specifying a custom queryset calling `filter()` and `exlclude()` as necessary, but #4255 updated the form fields to populate results by making independent REST API requests. This allowed for much better performance when dealing with large sets of objects at the cost of flexibility. One current partial workaround is to use NetBox's internal APISelect widget, however this component is not part of any public API offered by NetBox and is therefore not supported. Another workaround is to create a slightly customized form field which uses a direct ORM call to pre-populate the selection list (as opposed to a REST API call), however as discussed in #4255 this imposes sometimes severe performance penalties. This issue seeks to identify and implement a proper, documented solution. Although a precise solution remains to be identified, the overall goal is to replicate the functionality currently provided by the APISelect widget in a way that's less verbose and more convenient for users. Mechanisms are needed to provide control over the following: * The string used for displaying the object in the selection list * The value used to represent the object within the form data (e.g. a primary key or slug) * How to determine whether a list option is disabled * Additional query parameters used to arbitrarily restrict the list of objects * Applying a query parameter based on the value of another field within the form (field chaining) ### Use Case When writing a custom script in which the user is prompted to select one or more NetBox objects, it may be desirable to limit the choices presented to the user. For example, when presenting the user with a list of available sites, the custom script author may want to restrict the list to show only sites in the "active" status. This is not currently possible using ObjectVar as described in the documentation. ### Database Changes None ### External Dependencies None anticipated
adam added the status: acceptedtype: featurebeta labels 2025-12-29 18:32:22 +01:00
adam closed this issue 2025-12-29 18:32:22 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3968