Exception trying to define an event rule with a custom script #9545

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

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

Originally assigned to: @arthanson on GitHub.

Deployment Type

Self-hosted

NetBox Version

4.0.0-beta2

Python Version

3.11

Steps to Reproduce

  1. Create a test custom script
from extras.scripts import Script

class Test(Script):
    class Meta:
        name = "Test Script"

    def run(self, data, commit):
        self.log_info("Test script executed")
  1. Upload the script to the NetBox server
  2. Run the script manually to verify that it's working
    Screenshot 2024-04-25 at 20 37 48
  3. Now create a new Event Rule for any object type, set the action type to 'Script' and select the test script created above

Expected Behavior

An event rule is created

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/lib64/python3.11/site-packages/django/forms/models.py", line 552, in save
    self.instance.save()
    ^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/django/db/models/base.py", line 822, in save
    self.save_base(
    ^
  File "/opt/netbox/lib64/python3.11/site-packages/django/db/models/base.py", line 924, in save_base
    post_save.send(
    ^
  File "/opt/netbox/lib64/python3.11/site-packages/django/dispatch/dispatcher.py", line 189, in send
    response = receiver(signal=self, sender=sender, **named)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/extras/signals.py", line 122, in handle_changed_object
    enqueue_object(queue, instance, request.user, request.id, action)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/extras/events.py", line 65, in enqueue_object
    'data': serialize_for_event(instance),
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/extras/events.py", line 32, in serialize_for_event
    return serializer.data
           ^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/serializers.py", line 571, in data
    ret = super().data
          ^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/serializers.py", line 249, in data
    self._data = self.to_representation(self.instance)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/serializers.py", line 538, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/fields.py", line 1872, in to_representation
    return method(value)
           ^^^^^^^^^^^^^
  File "/opt/netbox/netbox/extras/api/serializers_/events.py", line 51, in get_action_object
    return ScriptSerializer(instance, nested=True, context=context).data
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/serializers.py", line 571, in data
    ret = super().data
          ^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/serializers.py", line 249, in data
    self._data = self.to_representation(self.instance)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/serializers.py", line 538, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/relations.py", line 396, in to_representation
    url = self.get_url(value, self.view_name, request, format)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/relations.py", line 332, in get_url
    lookup_value = getattr(obj, self.lookup_field)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Exception Type: AttributeError at /extras/event-rules/add/
Exception Value: 'Test' object has no attribute 'pk'
Originally created by @peteeckel on GitHub (Apr 25, 2024). Originally assigned to: @arthanson on GitHub. ### Deployment Type Self-hosted ### NetBox Version 4.0.0-beta2 ### Python Version 3.11 ### Steps to Reproduce 1. Create a test custom script ```python from extras.scripts import Script class Test(Script): class Meta: name = "Test Script" def run(self, data, commit): self.log_info("Test script executed") ``` 2. Upload the script to the NetBox server 3. Run the script manually to verify that it's working <img width="1104" alt="Screenshot 2024-04-25 at 20 37 48" src="https://github.com/netbox-community/netbox/assets/6815386/ba14ee58-169a-481c-98d0-3c48e470c1a0"> 4. Now create a new Event Rule for any object type, set the action type to 'Script' and select the test script created above ### Expected Behavior An event rule is created ### 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/lib64/python3.11/site-packages/django/forms/models.py", line 552, in save self.instance.save() ^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/django/db/models/base.py", line 822, in save self.save_base( ^ File "/opt/netbox/lib64/python3.11/site-packages/django/db/models/base.py", line 924, in save_base post_save.send( ^ File "/opt/netbox/lib64/python3.11/site-packages/django/dispatch/dispatcher.py", line 189, in send response = receiver(signal=self, sender=sender, **named) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/netbox/extras/signals.py", line 122, in handle_changed_object enqueue_object(queue, instance, request.user, request.id, action) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/netbox/extras/events.py", line 65, in enqueue_object 'data': serialize_for_event(instance), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/netbox/extras/events.py", line 32, in serialize_for_event return serializer.data ^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/serializers.py", line 571, in data ret = super().data ^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/serializers.py", line 249, in data self._data = self.to_representation(self.instance) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/serializers.py", line 538, in to_representation ret[field.field_name] = field.to_representation(attribute) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/fields.py", line 1872, in to_representation return method(value) ^^^^^^^^^^^^^ File "/opt/netbox/netbox/extras/api/serializers_/events.py", line 51, in get_action_object return ScriptSerializer(instance, nested=True, context=context).data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/serializers.py", line 571, in data ret = super().data ^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/serializers.py", line 249, in data self._data = self.to_representation(self.instance) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/serializers.py", line 538, in to_representation ret[field.field_name] = field.to_representation(attribute) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/relations.py", line 396, in to_representation url = self.get_url(value, self.view_name, request, format) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/lib64/python3.11/site-packages/rest_framework/relations.py", line 332, in get_url lookup_value = getattr(obj, self.lookup_field) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Exception Type: AttributeError at /extras/event-rules/add/ Exception Value: 'Test' object has no attribute 'pk' ```
adam added the type: bugstatus: acceptedbetaseverity: mediumtopic: event rules labels 2025-12-29 20:51:17 +01:00
adam closed this issue 2025-12-29 20:51:17 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9545