ObjectVar Prefix.objects.all() not displaying any prefixes in custom scripts fields. #3622

Closed
opened 2025-12-29 18:30:13 +01:00 by adam · 3 comments
Owner

Originally created by @phantompackets on GitHub (Apr 29, 2020).

Environment

  • Python version: python3.6
  • NetBox version: 2.7.12

Steps to Reproduce

  1. Create a custom script with ObjectVar as follows:
    prefix_assignment = ObjectVar(
    description="Select Parent Prefix to be assigned.",
    queryset = Prefix.objects.all()
  1. Place script in '/opt/netbox/netbox/scripts/'
  2. Go to url "http://netbox.test/extras/scripts"
  3. Click on script, click on ObjectVar field
  4. Nothing drops down.

Expected Behavior

A list of created prefixes would drop down in the ObjectVar field set with Prefix.objects.all()

Other objects query fine via the same syntax i.e VRF.objects.all(), Tenants.objects.all()

Observed Behavior

No prefixes are observed in the drop down list
Django queries from shell work fine

>>> Prefix.objects.all()
<PrefixQuerySet [<Prefix: 10.0.0.0/8>, <Prefix: 10.6.6.0/23>]>


>>> for prefix in Prefix.objects.all():
...     print (prefix.prefix, prefix.id)
... 
10.0.0.0/8 3
10.6.6.0/23 2

It also does not show prefixes when using filters or specifying a specific attribute, ie. 'id' or 'prefix', it seems to be only for Prefix queries.

Originally created by @phantompackets on GitHub (Apr 29, 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, and that any plugins have been disabled. --> ### Environment * Python version: python3.6 * NetBox version: 2.7.12 <!-- Describe in detail the exact steps that someone else can take to reproduce this bug using the current stable release of NetBox. 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 client library such as pynetbox. --> ### Steps to Reproduce 1. Create a custom script with ObjectVar as follows: ``` prefix_assignment = ObjectVar( description="Select Parent Prefix to be assigned.", queryset = Prefix.objects.all() ``` 2. Place script in '/opt/netbox/netbox/scripts/' 3. Go to url "http://netbox.test/extras/scripts" 4. Click on script, click on ObjectVar field 5. Nothing drops down. <!-- What did you expect to happen? --> ### Expected Behavior A list of created prefixes would drop down in the ObjectVar field set with Prefix.objects.all() Other objects query fine via the same syntax i.e VRF.objects.all(), Tenants.objects.all() <!-- What happened instead? --> ### Observed Behavior No prefixes are observed in the drop down list Django queries from shell work fine ``` >>> Prefix.objects.all() <PrefixQuerySet [<Prefix: 10.0.0.0/8>, <Prefix: 10.6.6.0/23>]> >>> for prefix in Prefix.objects.all(): ... print (prefix.prefix, prefix.id) ... 10.0.0.0/8 3 10.6.6.0/23 2 ``` It also does not show prefixes when using filters or specifying a specific attribute, ie. 'id' or 'prefix', it seems to be only for Prefix queries.
adam added the status: duplicate label 2025-12-29 18:30:13 +01:00
adam closed this issue 2025-12-29 18:30:13 +01:00
Author
Owner

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

This is related to #4463, you have to use something like this to make them appear:

 widget=APISelect(
            api_url='/api/ipam/aggregates/',
            display_field='prefix',
        )

The problem is that the returned object doesn't contain a "name" field and so all the entries get ignored / collapsed to the empty value by the UI control. Using the APISelect widget lets you specify the displayed field and then the control behaves better.

@ctrowat commented on GitHub (Apr 30, 2020): This is related to #4463, you have to use something like this to make them appear: ``` widget=APISelect( api_url='/api/ipam/aggregates/', display_field='prefix', ) ``` The problem is that the returned object doesn't contain a "name" field and so all the entries get ignored / collapsed to the empty value by the UI control. Using the APISelect widget lets you specify the displayed field and then the control behaves better.
Author
Owner

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

Marking this as a duplicate since the root issue has been raised already.

@jeremystretch commented on GitHub (May 6, 2020): Marking this as a duplicate since the root issue has been raised already.
Author
Owner

@phantompackets commented on GitHub (May 20, 2020):

This is related to #4463, you have to use something like this to make them appear:

 widget=APISelect(
            api_url='/api/ipam/aggregates/',
            display_field='prefix',
        )

The problem is that the returned object doesn't contain a "name" field and so all the entries get ignored / collapsed to the empty value by the UI control. Using the APISelect widget lets you specify the displayed field and then the control behaves better.

I am getting an error when using widgets on v2.7.12

<class 'NameError'>

name 'APISelect' is not defined

do i need to specifically import the widget in the custom script?

@phantompackets commented on GitHub (May 20, 2020): > This is related to #4463, you have to use something like this to make them appear: > > ``` > widget=APISelect( > api_url='/api/ipam/aggregates/', > display_field='prefix', > ) > ``` > > The problem is that the returned object doesn't contain a "name" field and so all the entries get ignored / collapsed to the empty value by the UI control. Using the APISelect widget lets you specify the displayed field and then the control behaves better. I am getting an error when using widgets on v2.7.12 <class 'NameError'> name 'APISelect' is not defined do i need to specifically import the widget in the custom script?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3622