Data is not passed when using runscript #10842

Closed
opened 2025-12-29 21:36:37 +01:00 by adam · 2 comments
Owner

Originally created by @krox2 on GitHub (Mar 4, 2025).

Deployment Type

Self-hosted

NetBox Version

v4.2.4

Python Version

3.12

Steps to Reproduce

  1. create a test custom script
$ cat data_source/test.py 
from extras.scripts import Script

class test(Script):

    def run(self, data, commit):
        print(f"data: {data}")
  1. run the custom script and pass data to it using runscript --data ...
$ ./venv/bin/python3 ./netbox/manage.py runscript -v 3 --data '{"site":1}' test.test

I've reverted the change #18612 to fix it locally

--- netbox/extras/management/commands/runscript.py.org  2025-03-04 13:50:41.594946866 +0000
+++ netbox/extras/management/commands/runscript.py      2025-03-04 14:00:21.957497595 +0000
@@ -91,7 +91,8 @@
             instance=script_obj,
             user=user,
             immediate=True,
-            data=form.cleaned_data,
+            #data=form.cleaned_data, this breaks passing data to the script as from.cleaned_data always returns an empty dict, introduced in netbox 4.2.4
+            data=data,
             request=NetBoxFakeRequest({
                 'META': {},
                 'POST': data,

Expected Behavior

$ ./venv/bin/python3 ./netbox/manage.py runscript -v 3 --data '{"site":1}' test.test
[2025-03-04 14:00:35,804][INFO] - Running script (commit=False)
data: {'site': 1}
[2025-03-04 14:00:35,805][INFO] - Database changes have been reverted automatically.
[2025-03-04 14:00:35,811][INFO] - Script completed in 0 minutes, 0.01 seconds

Observed Behavior

$ ./venv/bin/python3 ./netbox/manage.py runscript -v 3 --data '{"site":1}' test.test
[2025-03-04 13:58:59,889][INFO] - Running script (commit=False)
data: {}
[2025-03-04 13:58:59,890][INFO] - Database changes have been reverted automatically.
[2025-03-04 13:58:59,896][INFO] - Script completed in 0 minutes, 0.01 seconds
Originally created by @krox2 on GitHub (Mar 4, 2025). ### Deployment Type Self-hosted ### NetBox Version v4.2.4 ### Python Version 3.12 ### Steps to Reproduce 1. create a test custom script ``` $ cat data_source/test.py from extras.scripts import Script class test(Script): def run(self, data, commit): print(f"data: {data}") ``` 2. run the custom script and pass data to it using runscript --data ... ``` $ ./venv/bin/python3 ./netbox/manage.py runscript -v 3 --data '{"site":1}' test.test ``` I've reverted the change #18612 to fix it locally ``` --- netbox/extras/management/commands/runscript.py.org 2025-03-04 13:50:41.594946866 +0000 +++ netbox/extras/management/commands/runscript.py 2025-03-04 14:00:21.957497595 +0000 @@ -91,7 +91,8 @@ instance=script_obj, user=user, immediate=True, - data=form.cleaned_data, + #data=form.cleaned_data, this breaks passing data to the script as from.cleaned_data always returns an empty dict, introduced in netbox 4.2.4 + data=data, request=NetBoxFakeRequest({ 'META': {}, 'POST': data, ``` ### Expected Behavior ``` $ ./venv/bin/python3 ./netbox/manage.py runscript -v 3 --data '{"site":1}' test.test [2025-03-04 14:00:35,804][INFO] - Running script (commit=False) data: {'site': 1} [2025-03-04 14:00:35,805][INFO] - Database changes have been reverted automatically. [2025-03-04 14:00:35,811][INFO] - Script completed in 0 minutes, 0.01 seconds ``` ### Observed Behavior ``` $ ./venv/bin/python3 ./netbox/manage.py runscript -v 3 --data '{"site":1}' test.test [2025-03-04 13:58:59,889][INFO] - Running script (commit=False) data: {} [2025-03-04 13:58:59,890][INFO] - Database changes have been reverted automatically. [2025-03-04 13:58:59,896][INFO] - Script completed in 0 minutes, 0.01 seconds ```
adam added the type: bug label 2025-12-29 21:36:37 +01:00
adam closed this issue 2025-12-29 21:36:37 +01:00
Author
Owner

@arthanson commented on GitHub (Mar 4, 2025):

The change was to access the cleaned data so just the parameters defined in the script are accessible through data, so you will need to define Site as a param in the script as follows:

    site = ObjectVar(
        model=Site,
        required=True,
    )

to access it. The previous behavior, of including all of the passed in data without cleaning it was an oversight.

@arthanson commented on GitHub (Mar 4, 2025): The change was to access the cleaned data so just the parameters defined in the script are accessible through data, so you will need to define Site as a param in the script as follows: ``` site = ObjectVar( model=Site, required=True, ) ``` to access it. The previous behavior, of including all of the passed in data without cleaning it was an oversight.
Author
Owner

@krox2 commented on GitHub (Mar 4, 2025):

Thanks for the help.
I can't make it work when passing my own string variable I used in my scripts.

@krox2 commented on GitHub (Mar 4, 2025): Thanks for the help. I can't make it work when passing my own string variable I used in my scripts.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#10842