Unable to create TunnelTermination in Custom Script #9452

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

Originally created by @hackiero on GitHub (Apr 8, 2024).

Originally assigned to: @arthanson on GitHub.

Deployment Type

Self-hosted

NetBox Version

v3.7.5

Python Version

3.11

Steps to Reproduce

  1. Create VPN Tunnel, or have existing VPN tunnel with no terminations
  2. Import Custom Script
from core.models import ContentType
from vpn.models import IPSecProfile, Tunnel, TunnelTermination
from extras.scripts import Script, ObjectVar
name = "Tunnel Termination Test"
class TTTest(Script):
    class Meta:
        name = "VPN Tunnel Termination Test"

    tun = ObjectVar( model = Tunnel, default = 490 )

    def run(self, data, commit):
        tun = data['tun']
        tta = TunnelTermination(
            role = "hub",
            termination_type = ContentType.objects.get(model="interface"),
            tunnel = tun
        )
        tta.full_clean()
        tta.save()
  1. Run script, with commit, choose Tunnel object

Expected Behavior

A new VPN Termination object will be created.

Observed Behavior

Script complete successfully without Commit option selected.
With Commit, script fails with:

An exception occurred: AttributeError: 'NoneType' object has no attribute '_meta'

Traceback (most recent call last):
  File "/opt/netbox/netbox/extras/scripts.py", line 512, in _run_script
    script.output = script.run(data=data, commit=commit)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/scripts/palo-test.py", line 24, in run
    tta.save()
  File "/opt/netbox/venv/lib/python3.11/site-packages/django/db/models/base.py", line 814, in save
    self.save_base(
  File "/opt/netbox/venv/lib/python3.11/site-packages/django/db/models/base.py", line 892, in save_base
    post_save.send(
  File "/opt/netbox/venv/lib/python3.11/site-packages/django/dispatch/dispatcher.py", line 176, in send
    return [
           ^
  File "/opt/netbox/venv/lib/python3.11/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/extras/signals.py", line 97, in handle_changed_object
    enqueue_object(queue, instance, request.user, request.id, action)
  File "/opt/netbox/netbox/extras/events.py", line 68, in enqueue_object
    'data': serialize_for_event(instance),
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/extras/events.py", line 35, in serialize_for_event
    return serializer.data
           ^^^^^^^^^^^^^^^
  File "/opt/netbox/venv/lib/python3.11/site-packages/rest_framework/serializers.py", line 555, in data
    ret = super().data
          ^^^^^^^^^^^^
  File "/opt/netbox/venv/lib/python3.11/site-packages/rest_framework/serializers.py", line 253, in data
    self._data = self.to_representation(self.instance)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/venv/lib/python3.11/site-packages/rest_framework/serializers.py", line 522, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/venv/lib/python3.11/site-packages/rest_framework/fields.py", line 1838, in to_representation
    return method(value)
           ^^^^^^^^^^^^^
  File "/opt/netbox/netbox/vpn/api/serializers.py", line 101, in get_termination
    serializer = get_serializer_for_model(obj.termination, prefix=NESTED_SERIALIZER_PREFIX)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/utilities/api.py", line 26, in get_serializer_for_model
    app_name, model_name = model._meta.label.split('.')
                           ^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute '_meta'

Running similar code in nbshell works as expected:

### NetBox interactive shell (0dbb252f4afe)
### Python 3.11.4 | Django 4.2.11 | NetBox 3.7.5
### lsmodels() will show available models. Use help(<model>) for more info.
>>> tun = Tunnel.objects.get(id=490)
>>> tta = TunnelTermination(
...     role = "hub",
...     termination_type = ContentType.objects.get(model="interface"),
...     tunnel = tun
... )
>>> tta.save()
>>>
Originally created by @hackiero on GitHub (Apr 8, 2024). Originally assigned to: @arthanson on GitHub. ### Deployment Type Self-hosted ### NetBox Version v3.7.5 ### Python Version 3.11 ### Steps to Reproduce 1. Create VPN Tunnel, or have existing VPN tunnel with no terminations 2. Import Custom Script ```python from core.models import ContentType from vpn.models import IPSecProfile, Tunnel, TunnelTermination from extras.scripts import Script, ObjectVar name = "Tunnel Termination Test" class TTTest(Script): class Meta: name = "VPN Tunnel Termination Test" tun = ObjectVar( model = Tunnel, default = 490 ) def run(self, data, commit): tun = data['tun'] tta = TunnelTermination( role = "hub", termination_type = ContentType.objects.get(model="interface"), tunnel = tun ) tta.full_clean() tta.save() ``` 3. Run script, with commit, choose Tunnel object ### Expected Behavior A new VPN Termination object will be created. ### Observed Behavior Script complete successfully without Commit option selected. With Commit, script fails with: ``` An exception occurred: AttributeError: 'NoneType' object has no attribute '_meta' Traceback (most recent call last): File "/opt/netbox/netbox/extras/scripts.py", line 512, in _run_script script.output = script.run(data=data, commit=commit) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/netbox/scripts/palo-test.py", line 24, in run tta.save() File "/opt/netbox/venv/lib/python3.11/site-packages/django/db/models/base.py", line 814, in save self.save_base( File "/opt/netbox/venv/lib/python3.11/site-packages/django/db/models/base.py", line 892, in save_base post_save.send( File "/opt/netbox/venv/lib/python3.11/site-packages/django/dispatch/dispatcher.py", line 176, in send return [ ^ File "/opt/netbox/venv/lib/python3.11/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp> (receiver, receiver(signal=self, sender=sender, **named)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/netbox/extras/signals.py", line 97, in handle_changed_object enqueue_object(queue, instance, request.user, request.id, action) File "/opt/netbox/netbox/extras/events.py", line 68, in enqueue_object 'data': serialize_for_event(instance), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/netbox/extras/events.py", line 35, in serialize_for_event return serializer.data ^^^^^^^^^^^^^^^ File "/opt/netbox/venv/lib/python3.11/site-packages/rest_framework/serializers.py", line 555, in data ret = super().data ^^^^^^^^^^^^ File "/opt/netbox/venv/lib/python3.11/site-packages/rest_framework/serializers.py", line 253, in data self._data = self.to_representation(self.instance) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/venv/lib/python3.11/site-packages/rest_framework/serializers.py", line 522, in to_representation ret[field.field_name] = field.to_representation(attribute) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/venv/lib/python3.11/site-packages/rest_framework/fields.py", line 1838, in to_representation return method(value) ^^^^^^^^^^^^^ File "/opt/netbox/netbox/vpn/api/serializers.py", line 101, in get_termination serializer = get_serializer_for_model(obj.termination, prefix=NESTED_SERIALIZER_PREFIX) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/netbox/netbox/utilities/api.py", line 26, in get_serializer_for_model app_name, model_name = model._meta.label.split('.') ^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute '_meta' ``` Running similar code in nbshell works as expected: ```python ### NetBox interactive shell (0dbb252f4afe) ### Python 3.11.4 | Django 4.2.11 | NetBox 3.7.5 ### lsmodels() will show available models. Use help(<model>) for more info. >>> tun = Tunnel.objects.get(id=490) >>> tta = TunnelTermination( ... role = "hub", ... termination_type = ContentType.objects.get(model="interface"), ... tunnel = tun ... ) >>> tta.save() >>> ```
adam added the type: bugstatus: acceptedseverity: low labels 2025-12-29 20:50:05 +01:00
adam closed this issue 2025-12-29 20:50:06 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9452