Create a VLANGroup in a script / in shell #18206
-
Hello, I am trying to create VLANGroups objects with a script. VLANGroup.objects.create(
vid_ranges="1",
name="test",
slug="test"
) If I want VlanIDs Range = 100-150 VLANGroup.objects.create(
vid_ranges="100-150",
name="test",
slug="test"
) But it is not working >>> VLANGroup.objects.create(vid_ranges="105-110", name="aaaaTESSSSSSSSSSSST", slug="aaaatest")
Traceback (most recent call last):
File "/usr/lib/python3.12/code.py", line 90, in runcode
exec(code, self.locals)
File "<console>", line 1, in <module>
File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/query.py", line 679, in create
obj.save(force_insert=True, using=self.db)
File "/opt/netbox/netbox/ipam/models/vlans.py", line 129, in save
self._total_vlan_ids += vid_range.upper - vid_range.lower + 1
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'builtin_function_or_method'
>>> VLANGroup.objects.create(vid_ranges="100", name="aaaaTESSSSSSSSSSSST", slug="aaaatest")
Traceback (most recent call last):
File "/usr/lib/python3.12/code.py", line 90, in runcode
exec(code, self.locals)
File "<console>", line 1, in <module>
File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/query.py", line 679, in create
obj.save(force_insert=True, using=self.db)
File "/opt/netbox/netbox/ipam/models/vlans.py", line 129, in save
self._total_vlan_ids += vid_range.upper - vid_range.lower + 1
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'builtin_function_or_method' Thank you for your help !
from collections import namedtuple
Range = namedtuple("Range", ["lower", "upper"])
VLANGroup.objects.create(
vid_ranges=[
Range(lower=100, upper=101),
Range(lower=102, upper=151),
],
name="aaaaTESSSSSSSSSSSST",
slug="aaaatest"
)
Traceback (most recent call last):
File "/opt/netbox/netbox/extras/jobs.py", line 43, in run_script
script.output = script.run(data, commit)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/netbox/netbox/scripts/import_vlangroup.py", line 53, in run
VLANGroup.objects.create(name=vlan_group_name, slug=slugify(vlan_group_name), vid_ranges=[Range(lower=2000, upper=2099)], scope=site)
File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/query.py", line 679, in create
obj.save(force_insert=True, using=self.db)
File "/opt/netbox/netbox/ipam/models/vlans.py", line 131, in save
super().save(*args, **kwargs)
File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/base.py", line 822, in save
self.save_base(
File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/base.py", line 924, in save_base
post_save.send(
File "/opt/netbox/venv/lib/python3.12/site-packages/django/dispatch/dispatcher.py", line 189, in send
response = receiver(signal=self, sender=sender, **named)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/netbox/netbox/core/signals.py", line 79, in handle_changed_object
objectchange = instance.to_objectchange(action)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/netbox/netbox/netbox/models/features.py", line 110, in to_objectchange
objectchange.postchange_data = self.serialize_object(exclude=exclude)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/netbox/netbox/netbox/models/features.py", line 76, in serialize_object
return serialize_object(self, exclude=exclude or [])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/netbox/netbox/utilities/serialization.py", line 27, in serialize_object
json_str = serializers.serialize('json', [obj])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/netbox/venv/lib/python3.12/site-packages/django/core/serializers/__init__.py", line 134, in serialize
s.serialize(queryset, **options)
File "/opt/netbox/venv/lib/python3.12/site-packages/django/core/serializers/base.py", line 131, in serialize
self.handle_field(obj, field)
File "/opt/netbox/venv/lib/python3.12/site-packages/django/core/serializers/python.py", line 49, in handle_field
self._current[field.name] = self._value_from_field(obj, field)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/netbox/netbox/utilities/serializers/json.py", line 21, in _value_from_field
return value if is_protected_type(value) else field.value_to_string(obj)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/netbox/venv/lib/python3.12/site-packages/django/contrib/postgres/fields/array.py", line 171, in value_to_string
values.append(base_field.value_to_string(obj))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/netbox/venv/lib/python3.12/site-packages/django/contrib/postgres/fields/ranges.py", line 118, in value_to_string
if value.isempty:
^^^^^^^^^^^^^
AttributeError: 'Range' object has no attribute 'isempty' If you have any ideas for me, let me know 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
From the source: vid_ranges is an ArrayField of IntegerRangeField, and the default value is a list containing a single Postgres NumericRange (which is not the same as a the range tuple you manufactured) There is a utility function netbox.utilities.data.string_to_ranges which will convert the string format like "100-119,150-159" to a list of NumericRanges, so it's probably easiest if you just use that, but otherwise it should work if you use NumericRange imported from django.db.backends.postgresql.psycopg_any |
Beta Was this translation helpful? Give feedback.
From the source:
https://github.com/netbox-community/netbox/blob/v4.1.7/netbox/ipam/models/vlans.py#L58-L62
vid_ranges is an ArrayField of IntegerRangeField, and the default value is a list containing a single Postgres NumericRange (which is not the same as a the range tuple you manufactured)
There is a utility function netbox.utilities.data.string_to_ranges which will convert the string format like "100-119,150-159" to a list of NumericRanges, so it's probably easiest if you just use that, but otherwise it should work if you use NumericRange imported from django.db.backends.postgresql.psycopg_any