DeviceType not rendering in ObjectVar #3543

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

Originally created by @rodvand on GitHub (Apr 6, 2020).

Environment

  • Python version: 3.6.8
  • NetBox version: 2.7.11

Steps to Reproduce

  1. Create a custom script using ObjectVar and DeviceType as queryset
from extras.script import Script
from dcim.models import DeviceType

class Test(Script):
    dt = ObjectVar(
        queryset = DeviceType.objects.all()
    )

def run(self, data, commit):
    pass

Expected Behavior

Expected to be able to select a device type from the device type library in the dropdown menu.

Observed Behavior

No entries showing up in the dropdown menu.

Originally created by @rodvand on GitHub (Apr 6, 2020). ### Environment * Python version: 3.6.8 * NetBox version: 2.7.11 ### Steps to Reproduce 1. Create a custom script using ObjectVar and DeviceType as queryset ``` from extras.script import Script from dcim.models import DeviceType class Test(Script): dt = ObjectVar( queryset = DeviceType.objects.all() ) def run(self, data, commit): pass ``` ### Expected Behavior Expected to be able to select a device type from the device type library in the dropdown menu. ### Observed Behavior No entries showing up in the dropdown menu.
adam closed this issue 2025-12-29 18:29:47 +01:00
Author
Owner

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

The APISelect widget looks for a name field by default, which the DeviceType model does not have. You need to define it manually:

from extras.scripts import Script, ObjectVar
from dcim.models import DeviceType
from utilities.forms import APISelect


class Test(Script):
    dt = ObjectVar(
        queryset=DeviceType.objects.all(),
        widget=APISelect(
            display_field='model'
        )
    )

    def run(self, data, commit):
        pass

I realize this isn't the most convenient implementation, but it is what we currently have. If you get stuck again, remember that you can always look through the NetBox source for examples of what you're trying to achieve.

@jeremystretch commented on GitHub (Apr 6, 2020): The `APISelect` widget looks for a `name` field by default, which the DeviceType model does not have. You need to define it manually: ```python from extras.scripts import Script, ObjectVar from dcim.models import DeviceType from utilities.forms import APISelect class Test(Script): dt = ObjectVar( queryset=DeviceType.objects.all(), widget=APISelect( display_field='model' ) ) def run(self, data, commit): pass ``` I realize this isn't the most convenient implementation, but it is what we currently have. If you get stuck again, remember that you can always look through the NetBox source for examples of what you're trying to achieve.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3543