Error creating cable through the API #7468

Closed
opened 2025-12-29 20:23:45 +01:00 by adam · 3 comments
Owner

Originally created by @jschewebbn on GitHub (Jan 6, 2023).

NetBox version

v3.4.1

Python version

3.10

Steps to Reproduce

>>> port1
rear Bottom Right
>>> port2
rear392
>>> port1_type
'dcim.rearport'
>>> port2_type
'dcim.rearport'

>>> cable = netbox_utils.get_nb_session().dcim.cables.create(termination_a_type=port1_type, termination_a_id=port1.id, termination_b_type=port2_type, termination_b_id=port2.id)

Expected Behavior

Cable created.

Observed Behavior

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jschewe/py-envs/bi-scripts/lib/python3.10/site-packages/pynetbox/core/endpoint.py", line 318, in create
    ).post(args[0] if args else kwargs)
  File "/home/jschewe/py-envs/bi-scripts/lib/python3.10/site-packages/pynetbox/core/query.py", line 396, in post
    return self._make_call(verb="post", data=data)
  File "/home/jschewe/py-envs/bi-scripts/lib/python3.10/site-packages/pynetbox/core/query.py", line 284, in _make_call
    raise RequestError(req)
pynetbox.core.query.RequestError: The request failed with code 500 Internal Server Error but more specific details were not returned in json. Check the NetBox Logs or investigate this exception's error attribute.

Server error shows

Internal Server Error: /api/dcim/cables/
Traceback (most recent call last):
  File "/srv/netbox/venv/lib64/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/srv/netbox/venv/lib64/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/srv/netbox/venv/lib64/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "/srv/netbox/netbox/netbox/api/viewsets/__init__.py", line 118, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/mixins.py", line 18, in create
    serializer.is_valid(raise_exception=True)
  File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/serializers.py", line 227, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/serializers.py", line 429, in run_validation
    value = self.validate(value)
  File "/srv/netbox/netbox/netbox/api/serializers/base.py", line 41, in validate
    instance.full_clean()
  File "/srv/netbox/venv/lib64/python3.8/site-packages/django/db/models/base.py", line 1452, in full_clean
    self.clean()
  File "/srv/netbox/netbox/dcim/models/cables.py", line 148, in clean
    if self.pk is None and (not self.a_terminations or not self.b_terminations):
  File "/srv/netbox/netbox/dcim/models/cables.py", line 117, in a_terminations
    ct.termination for ct in self.terminations.all() if ct.cable_end == CableEndChoices.SIDE_A
  File "/srv/netbox/venv/lib64/python3.8/site-packages/django/db/models/manager.py", line 164, in all
    return self.get_queryset()
  File "/srv/netbox/venv/lib64/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 689, in get_queryset
    raise ValueError(
ValueError: 'Cable' instance needs to have a primary key value before this relationship can be used.
Originally created by @jschewebbn on GitHub (Jan 6, 2023). ### NetBox version v3.4.1 ### Python version 3.10 ### Steps to Reproduce ``` >>> port1 rear Bottom Right >>> port2 rear392 >>> port1_type 'dcim.rearport' >>> port2_type 'dcim.rearport' >>> cable = netbox_utils.get_nb_session().dcim.cables.create(termination_a_type=port1_type, termination_a_id=port1.id, termination_b_type=port2_type, termination_b_id=port2.id) ``` ### Expected Behavior Cable created. ### Observed Behavior ``` Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/jschewe/py-envs/bi-scripts/lib/python3.10/site-packages/pynetbox/core/endpoint.py", line 318, in create ).post(args[0] if args else kwargs) File "/home/jschewe/py-envs/bi-scripts/lib/python3.10/site-packages/pynetbox/core/query.py", line 396, in post return self._make_call(verb="post", data=data) File "/home/jschewe/py-envs/bi-scripts/lib/python3.10/site-packages/pynetbox/core/query.py", line 284, in _make_call raise RequestError(req) pynetbox.core.query.RequestError: The request failed with code 500 Internal Server Error but more specific details were not returned in json. Check the NetBox Logs or investigate this exception's error attribute. ``` Server error shows ``` Internal Server Error: /api/dcim/cables/ Traceback (most recent call last): File "/srv/netbox/venv/lib64/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) File "/srv/netbox/venv/lib64/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/srv/netbox/venv/lib64/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/viewsets.py", line 125, in view return self.dispatch(request, *args, **kwargs) File "/srv/netbox/netbox/netbox/api/viewsets/__init__.py", line 118, in dispatch return super().dispatch(request, *args, **kwargs) File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch response = self.handle_exception(exc) File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception self.raise_uncaught_exception(exc) File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception raise exc File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch response = handler(request, *args, **kwargs) File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/mixins.py", line 18, in create serializer.is_valid(raise_exception=True) File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/serializers.py", line 227, in is_valid self._validated_data = self.run_validation(self.initial_data) File "/srv/netbox/venv/lib64/python3.8/site-packages/rest_framework/serializers.py", line 429, in run_validation value = self.validate(value) File "/srv/netbox/netbox/netbox/api/serializers/base.py", line 41, in validate instance.full_clean() File "/srv/netbox/venv/lib64/python3.8/site-packages/django/db/models/base.py", line 1452, in full_clean self.clean() File "/srv/netbox/netbox/dcim/models/cables.py", line 148, in clean if self.pk is None and (not self.a_terminations or not self.b_terminations): File "/srv/netbox/netbox/dcim/models/cables.py", line 117, in a_terminations ct.termination for ct in self.terminations.all() if ct.cable_end == CableEndChoices.SIDE_A File "/srv/netbox/venv/lib64/python3.8/site-packages/django/db/models/manager.py", line 164, in all return self.get_queryset() File "/srv/netbox/venv/lib64/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 689, in get_queryset raise ValueError( ValueError: 'Cable' instance needs to have a primary key value before this relationship can be used. ```
adam added the type: bug label 2025-12-29 20:23:45 +01:00
adam closed this issue 2025-12-29 20:23:45 +01:00
Author
Owner

@kkthxbye-code commented on GitHub (Jan 6, 2023):

Duplicate of https://github.com/netbox-community/netbox/issues/11210

@kkthxbye-code commented on GitHub (Jan 6, 2023): Duplicate of https://github.com/netbox-community/netbox/issues/11210
Author
Owner

@kkthxbye-code commented on GitHub (Jan 6, 2023):

@jschewebbn - There's nothing preventing you from creating the cable, the issue is that the error message is wrong. To create the cable you need to update your code to the new API in 3.3:

https://github.com/netbox-community/netbox/releases/tag/v3.3.0

  • Several fields on the cable API serializers have been altered or removed to support multiple-object cable terminations:
Old Name Old Type New Name New Type
termination_a_type string Removed -
termination_b_type string Removed -
termination_a_id integer Removed -
termination_b_id integer Removed -
termination_a object a_terminations list
termination_b object b_terminations list
@kkthxbye-code commented on GitHub (Jan 6, 2023): @jschewebbn - There's nothing preventing you from creating the cable, the issue is that the error message is wrong. To create the cable you need to update your code to the new API in 3.3: https://github.com/netbox-community/netbox/releases/tag/v3.3.0 * Several fields on the cable API serializers have been altered or removed to support multiple-object cable terminations: | Old Name | Old Type | New Name | New Type | |----------------------|----------|-----------------------|----------| | `termination_a_type` | string | _Removed_ | - | | `termination_b_type` | string | _Removed_ | - | | `termination_a_id` | integer | _Removed_ | - | | `termination_b_id` | integer | _Removed_ | - | | `termination_a` | object | `a_terminations` | list | | `termination_b` | object | `b_terminations` | list |
Author
Owner

@jschewebbn commented on GitHub (Jan 6, 2023):

Interesting. I applied the patch and the server error went away. That gave me the error message that the a_terminations and b_terminations needed to be used. Now I've got that working. Thank you.

@jschewebbn commented on GitHub (Jan 6, 2023): Interesting. I applied the patch and the server error went away. That gave me the error message that the a_terminations and b_terminations needed to be used. Now I've got that working. Thank you.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7468