Exception trying to add a custom script #9461

Closed
opened 2025-12-29 20:50:13 +01:00 by adam · 0 comments
Owner

Originally created by @peteeckel on GitHub (Apr 9, 2024).

Originally assigned to: @arthanson on GitHub.

Deployment Type

Self-hosted

NetBox Version

v4.0-beta1

Python Version

3.11

Steps to Reproduce

  1. Create a file with this content:
from extras.scripts import Script


class Test(Script):

    class Meta:
        name = "Test"
        description = "This is a test script without any function"
        commit_default = True

    def run(self, data, commit):
        self.log_info("Script was executed")
  1. Go to the NetBox GUI and navigate to 'Customization/Scripts/Add' to add the script
  2. Go to 'Upload File/Choose File' and select the local file with the script for upload, then click 'Create'

Expected Behavior

The script is added to the list of installed scripts. This works perfectly in v3.7.5.

Observed Behavior

Traceback (most recent call last):
  File "/opt/netbox/lib64/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/netbox/views/generic/object_views.py", line 175, in dispatch
    return super().dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/netbox/views/generic/base.py", line 26, in dispatch
    return super().dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/utilities/views.py", line 108, in dispatch
    return super().dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/django/views/generic/base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/netbox/views/generic/object_views.py", line 269, in post
    obj = form.save()
          ^^^^^^^^^^^
  File "/opt/netbox/netbox/core/forms/model_forms.py", line 120, in save
    return super().save(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/django/forms/models.py", line 552, in save
    self.instance.save()
    ^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/extras/models/scripts.py", line 168, in save
    self.sync_classes()
    ^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/extras/models/scripts.py", line 157, in sync_classes
    Script.objects.create(
    ^
  File "/opt/netbox/lib64/python3.11/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/django/db/models/query.py", line 679, in create
    obj.save(force_insert=True, using=self.db)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/django/db/models/base.py", line 778, in save
    self._prepare_related_fields_for_save(operation_name="save")
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/django/db/models/base.py", line 1140, in _prepare_related_fields_for_save
    raise ValueError(
    ^

Exception Type: ValueError at /extras/scripts/add/
Exception Value: save() prohibited to prevent data loss due to unsaved related object 'module'.
Originally created by @peteeckel on GitHub (Apr 9, 2024). Originally assigned to: @arthanson on GitHub. ### Deployment Type Self-hosted ### NetBox Version v4.0-beta1 ### Python Version 3.11 ### Steps to Reproduce 1. Create a file with this content: ```python from extras.scripts import Script class Test(Script): class Meta: name = "Test" description = "This is a test script without any function" commit_default = True def run(self, data, commit): self.log_info("Script was executed") ``` 2. Go to the NetBox GUI and navigate to 'Customization/Scripts/Add' to add the script 3. Go to 'Upload File/Choose File' and select the local file with the script for upload, then click 'Create' ### Expected Behavior The script is added to the list of installed scripts. This works perfectly in v3.7.5. ### Observed Behavior ``` Traceback (most recent call last): File "/opt/netbox/lib64/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) ^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/django/views/generic/base.py", line 104, in view return self.dispatch(request, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/netbox/netbox/views/generic/object_views.py", line 175, in dispatch return super().dispatch(request, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/netbox/netbox/views/generic/base.py", line 26, in dispatch return super().dispatch(request, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/netbox/utilities/views.py", line 108, in dispatch return super().dispatch(request, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/django/views/generic/base.py", line 143, in dispatch return handler(request, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/netbox/netbox/views/generic/object_views.py", line 269, in post obj = form.save() ^^^^^^^^^^^ File "/opt/netbox/netbox/core/forms/model_forms.py", line 120, in save return super().save(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/django/forms/models.py", line 552, in save self.instance.save() ^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/netbox/extras/models/scripts.py", line 168, in save self.sync_classes() ^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/netbox/extras/models/scripts.py", line 157, in sync_classes Script.objects.create( ^ File "/opt/netbox/lib64/python3.11/site-packages/django/db/models/manager.py", line 87, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/django/db/models/query.py", line 679, in create obj.save(force_insert=True, using=self.db) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/django/db/models/base.py", line 778, in save self._prepare_related_fields_for_save(operation_name="save") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/django/db/models/base.py", line 1140, in _prepare_related_fields_for_save raise ValueError( ^ Exception Type: ValueError at /extras/scripts/add/ Exception Value: save() prohibited to prevent data loss due to unsaved related object 'module'. ```
adam added the type: bugstatus: acceptedbetaseverity: medium labels 2025-12-29 20:50:13 +01:00
adam closed this issue 2025-12-29 20:50:13 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9461