Skip to content

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

@Ikke

Description

@Ikke

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions