Custom Script data has different values via REST API run and via GUI run #9057

Closed
opened 2025-12-29 20:44:52 +01:00 by adam · 2 comments
Owner

Originally created by @amyasnikov on GitHub (Jan 8, 2024).

Deployment Type

Self-hosted

NetBox Version

v.3.7.0

Python Version

3.10

Steps to Reproduce

  1. Create Script
from extras.scripts import MultiObjectVar, Script
from dcim.models import Device


class MyScript(Script):
    my_var = MultiObjectVar(model=Device)

   def run(self, data, commit):
       print(data)
  1. Try to run it via REST and via GUI and check out console outputs. It will be something like {'my_var': [2, 4]} for REST API and something like {'my_var': <ConfigContextModelQuerySet [<Device: device2>, <Device: device4>]} for GUI run.

Expected Behavior

Both ways of executing a script lead to exactly the same contents of "data" variable inside the script (either primary keys or queryset).

Observed Behavior

For ObjectVar and MultiObjectVar the data is different. It happens due to:

  1. Passing of raw data for API:
    https://github.com/netbox-community/netbox/blob/develop/netbox/extras/api/views.py#L398

  2. Passing of cleaned data for GUI
    https://github.com/netbox-community/netbox/blob/develop/netbox/extras/views.py#L1273

Originally created by @amyasnikov on GitHub (Jan 8, 2024). ### Deployment Type Self-hosted ### NetBox Version v.3.7.0 ### Python Version 3.10 ### Steps to Reproduce 1. Create Script ``` from extras.scripts import MultiObjectVar, Script from dcim.models import Device class MyScript(Script): my_var = MultiObjectVar(model=Device) def run(self, data, commit): print(data) ``` 2. Try to run it via REST and via GUI and check out console outputs. It will be something like `{'my_var': [2, 4]}` for REST API and something like `{'my_var': <ConfigContextModelQuerySet [<Device: device2>, <Device: device4>]}` for GUI run. ### Expected Behavior Both ways of executing a script lead to exactly the same contents of "data" variable inside the script (either primary keys or queryset). ### Observed Behavior For ObjectVar and MultiObjectVar the data is different. It happens due to: 1. Passing of raw data for API: https://github.com/netbox-community/netbox/blob/develop/netbox/extras/api/views.py#L398 2. Passing of **cleaned** data for GUI https://github.com/netbox-community/netbox/blob/develop/netbox/extras/views.py#L1273
adam closed this issue 2025-12-29 20:44:52 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 8, 2024):

This is not a bug. You (the script author) are responsible for serializing any objects included in script output. By default, these are serialized to their primary keys. If you need assistance writing scripts, please open a discussion.

@jeremystretch commented on GitHub (Jan 8, 2024): This is not a bug. You (the script author) are responsible for serializing any objects included in script output. By default, these are serialized to their primary keys. If you need assistance writing scripts, please [open a discussion](https://github.com/netbox-community/netbox/discussions/new?category=help-wanted).
Author
Owner

@amyasnikov commented on GitHub (Jan 8, 2024):

@jeremystretch as a script author I'm not opposed to receiving primary keys, it's totally okay. I'm opposed to existing inconsistency in the way Netbox passes the arguments to my script.
If I run my script via GUI, I will get queryset (or model instance in case of ObjectVar)
If I run my script via REST API, I will get list of primary keys (or one PK for ObjectVar)
As a script author I don't want script parameters to depend on the way the script has been triggered.

Moreover, passing whole objects (ObjectVar case) as queue task parameters may cause race conditions and is not recommended.

@amyasnikov commented on GitHub (Jan 8, 2024): @jeremystretch as a script author I'm not opposed to receiving primary keys, it's totally okay. I'm opposed to **existing inconsistency** in the way Netbox passes the arguments to my script. If I run my script via GUI, I will get **queryset** (or model instance in case of ObjectVar) If I run my script via REST API, I will get **list of primary keys** (or one PK for ObjectVar) As a script author I don't want script parameters to depend on the way the script has been triggered. Moreover, passing whole objects (ObjectVar case) as queue task parameters may cause race conditions and is not recommended.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9057