Clarify ObjectVar behavior in Custom Scripts #3808

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

Originally created by @tyler-8 on GitHub (Jun 25, 2020).

Change Type

[X] Addition
[ ] Correction
[ ] Deprecation
[ ] Cleanup (formatting, typos, etc.)

Area

[ ] Installation instructions
[ ] Configuration parameters
[X] Functionality/features
[ ] REST API
[ ] Administration/development
[ ] Other

Proposed Changes

The ObjectVar field for Custom Scripts changed behaviors in recent months and the documentation could be improved to clarify the new functionality.

The example could be updated with this snippet to show the only built-in method for filtering:

switch_model = ObjectVar(
    description="Access switch model",
    queryset = DeviceType.objects.all(),  # filtering not supported here
    widget=APISelect(
        display_field="display_name",
        additional_query_params={
            "manufacturer": "cisco",
            "model": ['catalyst-3560x-48t', 'catalyst-3750x-48t'],  # list of slugs
        },
    ),
)

Under Default Options a think an expanded note explaining the usage of widget would be beneficial as well. Users cannot drop-in a Django widget 'out of the box' to change the behavior of the ObjectVar, they must create a new widget based on a Django widget.

Ideally an example of that could also be included here (I'll start looking in to that myself) but if anyone has that readily available we could include it as well.

The main things to be covered I think

  1. Without a custom widget, querysets can't use filter(), they can only use .all()
  2. Without a custom widget, filtering must be done using APISelect and the additional_query_params field
  3. Explanation of the display_field parameter for APISelect

Perhaps to cover 2 and 3 we include the docstring for APISelect?

Originally created by @tyler-8 on GitHub (Jun 25, 2020). <!-- NOTE: IF YOUR ISSUE DOES NOT FOLLOW THIS TEMPLATE, IT WILL BE CLOSED. Please indicate the nature of the change by placing an X in one of the boxes below. --> ### Change Type [X] Addition [ ] Correction [ ] Deprecation [ ] Cleanup (formatting, typos, etc.) ### Area [ ] Installation instructions [ ] Configuration parameters [X] Functionality/features [ ] REST API [ ] Administration/development [ ] Other <!-- Describe the proposed change(s). --> ### Proposed Changes The ObjectVar field for Custom Scripts changed behaviors in recent months and the [documentation](https://github.com/netbox-community/netbox/blob/9fc1e88d9f5d3a93cb0288a6aee85c6a2333152b/docs/additional-features/custom-scripts.md#custom-scripts) could be improved to clarify the new functionality. The example could be updated with this snippet to show the only built-in method for filtering: ```python switch_model = ObjectVar( description="Access switch model", queryset = DeviceType.objects.all(), # filtering not supported here widget=APISelect( display_field="display_name", additional_query_params={ "manufacturer": "cisco", "model": ['catalyst-3560x-48t', 'catalyst-3750x-48t'], # list of slugs }, ), ) ``` Under [Default Options](https://github.com/netbox-community/netbox/blob/9fc1e88d9f5d3a93cb0288a6aee85c6a2333152b/docs/additional-features/custom-scripts.md#default-options) a think an expanded note explaining the usage of widget would be beneficial as well. Users cannot drop-in a Django widget 'out of the box' to change the behavior of the ObjectVar, they must create a new widget based on a Django widget. Ideally an example of that could also be included here (I'll start looking in to that myself) but if anyone has that readily available we could include it as well. The main things to be covered I think 1) Without a custom widget, querysets can't use `filter()`, they can only use `.all()` 2) Without a custom widget, filtering must be done using `APISelect` and the `additional_query_params` field 3) Explanation of the `display_field` parameter for `APISelect` Perhaps to cover 2 and 3 we include the docstring for `APISelect`?
adam added the status: acceptedtype: documentation labels 2025-12-29 18:31:19 +01:00
adam closed this issue 2025-12-29 18:31:19 +01:00
Author
Owner

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

As discussed, APISelect is not and will not be part of the public API for custom scripts. #4789 has been opened to propose the functionality in play, but in the interim the documentation just needs to be updated to remove the outdated example.

@jeremystretch commented on GitHub (Jun 25, 2020): As discussed, APISelect is not and will not be part of the public API for custom scripts. #4789 has been opened to propose the functionality in play, but in the interim the documentation just needs to be updated to remove the outdated example.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3808