ObjectVar queryset returns all items - does not filter #3550

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

Originally created by @darcynz on GitHub (Apr 8, 2020).

Environment

  • Python version: 3.7
  • NetBox version: 2.7.11

Steps to Reproduce

  1. Added following code as per the Netbox New Branch Script example
    class Meta:
        name = "New Branch"
        description = "Provision a new branch site"
        fields = ['site_name', 'switch_count', 'switch_model']

    site_name = StringVar(
        description="Name of the new site"
    )
    switch_count = IntegerVar(
        description="Number of access switches to create"
    )
    switch_model = ObjectVar(
        description="Access switch model",
        queryset = DeviceType.objects.filter(
            manufacturer__name='Cisco',
            model__in=['Catalyst 3560X-48T', 'Catalyst 3750X-48T']
        ),
        widget=APISelect(display_field='model')
    )
  1. Go the script page and click the model type drop down.

Expected Behavior

Only 'Catalyst 3560X-48T', 'Catalyst 3750X-48T'] are displayed.

Observed Behavior

All model types are displayed.

FURTHER INFO ---
I've been having this issue on a Site.objects.Filter() i've created that i could not get to work (it would display all records.

Tried to replicate via the site script example as above. But no fields would display initially.

Read the previous article related to widget=APISelect(display_field='model') and added that. Then saw it also displayed all device types instead of just two.

Originally created by @darcynz on GitHub (Apr 8, 2020). <!-- NOTE: IF YOUR ISSUE DOES NOT FOLLOW THIS TEMPLATE, IT WILL BE CLOSED. This form is only for reproducible bugs. If you need assistance with NetBox installation, or if you have a general question, DO NOT open an issue. Instead, post to our mailing list: https://groups.google.com/forum/#!forum/netbox-discuss Please describe the environment in which you are running NetBox. Be sure that you are running an unmodified instance of the latest stable release before submitting a bug report. --> ### Environment * Python version: 3.7 <!-- Example: 3.7 --> * NetBox version: 2.7.11 <!-- Example: 2.7.11 --> <!-- Describe in detail the exact steps that someone else can take to reproduce this bug using the current stable release of NetBox (or the current beta release where applicable). Begin with the creation of any necessary database objects and call out every operation being performed explicitly. If reporting a bug in the REST API, be sure to reconstruct the raw HTTP request(s) being made: Don't rely on a wrapper like pynetbox. --> ### Steps to Reproduce 1. Added following code as per the Netbox New Branch Script example ``` class Meta: name = "New Branch" description = "Provision a new branch site" fields = ['site_name', 'switch_count', 'switch_model'] site_name = StringVar( description="Name of the new site" ) switch_count = IntegerVar( description="Number of access switches to create" ) switch_model = ObjectVar( description="Access switch model", queryset = DeviceType.objects.filter( manufacturer__name='Cisco', model__in=['Catalyst 3560X-48T', 'Catalyst 3750X-48T'] ), widget=APISelect(display_field='model') ) ``` 2. Go the script page and click the model type drop down. <!-- What did you expect to happen? --> ### Expected Behavior Only 'Catalyst 3560X-48T', 'Catalyst 3750X-48T'] are displayed. <!-- What happened instead? --> ### Observed Behavior All model types are displayed. FURTHER INFO --- I've been having this issue on a Site.objects.Filter() i've created that i could not get to work (it would display all records. Tried to replicate via the site script example as above. But no fields would display initially. Read the previous article related to widget=APISelect(display_field='model') and added that. Then saw it also displayed all device types instead of just two.
adam added the status: acceptedtype: documentation labels 2025-12-29 18:29:50 +01:00
adam closed this issue 2025-12-29 18:29:50 +01:00
Author
Owner

@darcynz commented on GitHub (Apr 8, 2020):

Further to the above, when you run the script it returns an error if the filtered objects are not selected. Indicating that the problem is just what items in the dropdown list are displayed.

@darcynz commented on GitHub (Apr 8, 2020): Further to the above, when you run the script it returns an error if the filtered objects are not selected. Indicating that the problem is just what items in the dropdown list are displayed.
Author
Owner

@DanSheps commented on GitHub (Apr 8, 2020):

Please update the version information

@DanSheps commented on GitHub (Apr 8, 2020): Please update the version information
Author
Owner

@darcynz commented on GitHub (Apr 8, 2020):

Done, sorry was commented out

@darcynz commented on GitHub (Apr 8, 2020): Done, sorry was commented out
Author
Owner

@kobayashi commented on GitHub (Apr 10, 2020):

To filter display objects, query parameters have to be in APISelect.

switch_model = ObjectVar(
    description="Access switch model",
    queryset=DeviceType.objects.all(),
    widget=APISelect(
        api_url='/api/dcim/device-types/',
        display_field='model',
        additional_query_params={'model': ['Catalyst 3560X-48T', 'Catalyst 3750X-48T']}
    ),

I will make PR to update custom script section

@kobayashi commented on GitHub (Apr 10, 2020): To filter display objects, query parameters have to be in `APISelect`. ``` switch_model = ObjectVar( description="Access switch model", queryset=DeviceType.objects.all(), widget=APISelect( api_url='/api/dcim/device-types/', display_field='model', additional_query_params={'model': ['Catalyst 3560X-48T', 'Catalyst 3750X-48T']} ), ``` I will make PR to update [custom script section](https://netbox.readthedocs.io/en/stable/additional-features/custom-scripts/)
Author
Owner

@darcynz commented on GitHub (Apr 15, 2020):

Thanks, got it working...

One question, can additional_query_params, take a Danjo Queries?
eg DeviceType.objects.all(Q(model='Catalyst 3560X-48T')

Regards, Darcy

On Sat, Apr 11, 2020 at 2:38 AM kobayashi notifications@github.com wrote:

To filter display objects, query parameters have to be in APISelect.

switch_model = ObjectVar(
description="Access switch model",
queryset=DeviceType.objects.all(),
widget=APISelect(
api_url='/api/dcim/device-types/',
display_field='model',
additional_query_params={'model': ['Catalyst 3560X-48T', 'Catalyst 3750X-48T']}
),

I will make PR to update custom script section
https://netbox.readthedocs.io/en/stable/additional-features/custom-scripts/


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/netbox-community/netbox/issues/4463#issuecomment-612057278,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AJOZLSWVFJOFMPZHYXHF453RL4VOPANCNFSM4MDV4C7A
.

@darcynz commented on GitHub (Apr 15, 2020): Thanks, got it working... One question, can additional_query_params, take a Danjo Queries? eg DeviceType.objects.all(Q(model='Catalyst 3560X-48T') Regards, Darcy On Sat, Apr 11, 2020 at 2:38 AM kobayashi <notifications@github.com> wrote: > To filter display objects, query parameters have to be in APISelect. > > switch_model = ObjectVar( > description="Access switch model", > queryset=DeviceType.objects.all(), > widget=APISelect( > api_url='/api/dcim/device-types/', > display_field='model', > additional_query_params={'model': ['Catalyst 3560X-48T', 'Catalyst 3750X-48T']} > ), > > > I will make PR to update custom script section > <https://netbox.readthedocs.io/en/stable/additional-features/custom-scripts/> > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > <https://github.com/netbox-community/netbox/issues/4463#issuecomment-612057278>, > or unsubscribe > <https://github.com/notifications/unsubscribe-auth/AJOZLSWVFJOFMPZHYXHF453RL4VOPANCNFSM4MDV4C7A> > . >
Author
Owner

@kobayashi commented on GitHub (Apr 20, 2020):

ObjectVar is filtered by APISelect widget which calls API to get objects. This has some parameters to filter objects. I will document these parameters also.

5aadfff1de/netbox/utilities/forms.py (L252-L270)

@kobayashi commented on GitHub (Apr 20, 2020): ObjectVar is filtered by `APISelect` widget which calls API to get objects. This has some parameters to filter objects. I will document these parameters also. https://github.com/netbox-community/netbox/blob/5aadfff1de2ba7a07faa327c519df4038bab6e7e/netbox/utilities/forms.py#L252-L270
Author
Owner

@ctrowat commented on GitHub (Apr 29, 2020):

This is maybe a little bit on the periphery but still related to the subject of this issue.

My problem occurs on the scripts page, before executing a given script, when selecting an object of a given type:

When clicking into the dropdown the repeated sequential requests are made for item information (?q=&limit=50&offset=<x>&brief=true) until it has fetched all objects of a given type. For example, when selecting Prefixes (of which we have a total of over 6000) this takes a substantial amount of time and does not seem like desired behaviour. On other pages when using similar controls to select items it only makes a single request initially and waits until you scroll to the bottom of the results or type text in the box to make any additional requests.

@ctrowat commented on GitHub (Apr 29, 2020): This is maybe a little bit on the periphery but still related to the subject of this issue. My problem occurs on the scripts page, before executing a given script, when selecting an object of a given type: When clicking into the dropdown the repeated sequential requests are made for item information (`?q=&limit=50&offset=<x>&brief=true`) until it has fetched all objects of a given type. For example, when selecting Prefixes (of which we have a total of over 6000) this takes a substantial amount of time and does not seem like desired behaviour. On other pages when using similar controls to select items it only makes a single request initially and waits until you scroll to the bottom of the results or type text in the box to make any additional requests.
Author
Owner

@tyler-8 commented on GitHub (Jun 29, 2020):

@kobayashi In light of https://github.com/netbox-community/netbox/issues/4791 - does this issue still fit in to the 'documentation' category or could this issue be used to cover creating a publicly available API for Custom Script ObjectVar filtering as mentioned here?

@tyler-8 commented on GitHub (Jun 29, 2020): @kobayashi In light of https://github.com/netbox-community/netbox/issues/4791 - does this issue still fit in to the 'documentation' category or could this issue be used to cover creating a publicly available API for Custom Script ObjectVar filtering as mentioned [here](https://github.com/netbox-community/netbox/pull/4509#issuecomment-616647816)?
Author
Owner

@jeremystretch commented on GitHub (Aug 12, 2020):

I've opened #4982 to address this functionality, which will entail its documentation as well.

@jeremystretch commented on GitHub (Aug 12, 2020): I've opened #4982 to address this functionality, which will entail its documentation as well.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3550