Dynamic form fields based on user input for custom scripts #3807

Closed
opened 2025-12-29 18:31:18 +01:00 by adam · 2 comments
Owner

Originally created by @Ikke on GitHub (Jun 25, 2020).

Environment

  • Python version: Example: 3.6.9
  • NetBox version: Example: 2.8.5

Proposed Functionality

This is a continuation of #3766, which asked about being able to filter form fields based on the input of other form fields.

One point that was not addressed as this comment. From what I could find, it is not possible to dynamically adjust the query used for a field based on what value is selected in another field.

I found that APISelect widget has a parameter called conditional_query_params, which seems to come pretty close, but as far as I can see you cannot use that to adjust the actual query values, only add static conditions based on another condition.

So it would be nice if we could refer to the selected value from another field, and use that value in the query params.

Something like

site = ObjectVar(
    description = "Site",
    queryset = Site.objects.all()
)

prefix = ObjectVar(
    queryset = Prefix.objects.all(),
    widget = APISelect(
        api_url="/api/ipam/prefixes/",
        dynamic_query_params={
            'site': 'site_id={{id}}'
        },
        display_field='prefix'
    )
)

Use Case

Same as #3766

For example: Have a form where you first select a site, and then select a prefix within that site.

Database Changes

None

External Dependencies

None

Originally created by @Ikke on GitHub (Jun 25, 2020). ### Environment * Python version: Example: 3.6.9 * NetBox version: Example: 2.8.5 ### Proposed Functionality This is a continuation of #3766, which asked about being able to filter form fields based on the input of other form fields. One point that was not addressed as [this comment](https://github.com/netbox-community/netbox/issues/3766#issuecomment-570544562). From what I could find, it is not possible to dynamically adjust the query used for a field based on what value is selected in another field. I found that `APISelect` widget has a parameter called `conditional_query_params`, which seems to come pretty close, but as far as I can see you cannot use that to adjust the actual query values, only add static conditions based on another condition. So it would be nice if we could refer to the selected value from another field, and use that value in the query params. Something like ```python site = ObjectVar( description = "Site", queryset = Site.objects.all() ) prefix = ObjectVar( queryset = Prefix.objects.all(), widget = APISelect( api_url="/api/ipam/prefixes/", dynamic_query_params={ 'site': 'site_id={{id}}' }, display_field='prefix' ) ) ``` ### Use Case Same as #3766 For example: Have a form where you first select a site, and then select a prefix within that site. ### Database Changes None ### External Dependencies None
adam closed this issue 2025-12-29 18:31:18 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jun 25, 2020):

APISelect is not part of NetBox's public API, and is not intended for consumption outside the core code base. It is subject to change at any time, without warning, and your scripts which use it are very likely to break when upgrading to a future release.

Please revise your issue to on the specific functionality you are proposing, without invoking components from outside the documented API for custom scripts.

@jeremystretch commented on GitHub (Jun 25, 2020): APISelect is not part of NetBox's public API, and is not intended for consumption outside the core code base. It is subject to change at any time, without warning, and your scripts which use it are very likely to break when upgrading to a future release. Please revise your issue to on the specific **functionality** you are proposing, without invoking components from outside the [documented API for custom scripts](https://netbox.readthedocs.io/en/stable/additional-features/custom-scripts/).
Author
Owner

@jeremystretch commented on GitHub (Jul 6, 2020):

Closed due to inactivity.

@jeremystretch commented on GitHub (Jul 6, 2020): Closed due to inactivity.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3807