Server side error when creating device with custom_fields through the api #1351

Closed
opened 2025-12-29 16:31:42 +01:00 by adam · 8 comments
Owner

Originally created by @kicker69101 on GitHub (Oct 26, 2017).

Issue type

[ ] Feature request
[ X ] Bug report
[ ] Documentation

Environment

  • Python version: 2.7.5
  • NetBox version: 2.1.5

Description

I'm getting a server side exception when creating an object with custom_fields. Here is the exception:

ERROR Internal Server Error: /api/dcim/devices/#012Traceback (most recent call last):#012 File "/usr/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner#012 response = get_response(request)#012 File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response#012 response = self.process_exception_by_middleware(e, request)#012 File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response#012 response = wrapped_callback(request, *callback_args, **callback_kwargs)#012 File "/usr/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view#012 return view_func(*args, **kwargs)#012 File "/usr/lib/python2.7/site-packages/rest_framework/viewsets.py", line 90, in view#012 return self.dispatch(request, *args, **kwargs)#012 File "/usr/lib/python2.7/site-packages/rest_framework/views.py", line 489, in dispatch#012 response = self.handle_exception(exc)#012 File "/usr/lib/python2.7/site-packages/rest_framework/views.py", line 449, in handle_exception#012 self.raise_uncaught_exception(exc)#012 File "/usr/lib/python2.7/site-packages/rest_framework/views.py", line 486, in dispatch#012 response = handler(request, *args, **kwargs)#012 File "/usr/lib/python2.7/site-packages/rest_framework/mixins.py", line 20, in create#012 serializer.is_valid(raise_exception=True)#012 File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py", line 237, in is_valid#012 self._validated_data = self.run_validation(self.initial_data)#012 File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py", line 432, in run_validation#012 value = self.to_internal_value(data)#012 File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py", line 462, in to_internal_value#012 validated_value = field.run_validation(primitive_value)#012 File "/usr/lib/python2.7/site-packages/rest_framework/fields.py", line 524, in run_validation#012 value = self.to_internal_value(data)#012 File "/opt/netbox/netbox/extras/api/customfields.py", line 30, in to_internal_value#012 for field_name, value in data.items():#012AttributeError: 'unicode' object has no attribute 'items'

Here is the payload:

{ "name":"***********", "device_type":1772, "device_role":31, "tenant":null, "platform":null, "serial":"#########", "asset_tag":null, "site":16, "rack":1133, "position":25, "face":0, "status":1, "primary_ip4":null, "primary_ip6":null, "comments":"", "custom_fields":{"rack_tables_id":42} }

Originally created by @kicker69101 on GitHub (Oct 26, 2017). <!-- Before opening a new issue, please search through the existing issues to see if your topic has already been addressed. Note that you may need to remove the "is:open" filter from the search bar to include closed issues. Check the appropriate type for your issue below by placing an x between the brackets. If none of the below apply, please raise your issue for discussion on our mailing list: https://groups.google.com/forum/#!forum/netbox-discuss Please note that issues which do not fall under any of the below categories will be closed. ---> ### Issue type [ ] Feature request <!-- Requesting the implementation of a new feature --> [ X ] Bug report <!-- Reporting unexpected or erroneous behavior --> [ ] Documentation <!-- Proposing a modification to the documentation --> <!-- Please describe the environment in which you are running NetBox. (Be sure to verify that you are running the latest stable release of NetBox before submitting a bug report.) --> ### Environment * Python version: 2.7.5 * NetBox version: 2.1.5 <!-- BUG REPORTS must include: * A list of the steps needed to reproduce the bug * A description of the expected behavior * Any relevant error messages (screenshots may also help) FEATURE REQUESTS must include: * A detailed description of the proposed functionality * A use case for the new feature * A rough description of any necessary changes to the database schema * Any relevant third-party libraries which would be needed --> ### Description I'm getting a server side exception when creating an object with custom_fields. Here is the exception: `ERROR Internal Server Error: /api/dcim/devices/#012Traceback (most recent call last):#012 File "/usr/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner#012 response = get_response(request)#012 File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response#012 response = self.process_exception_by_middleware(e, request)#012 File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response#012 response = wrapped_callback(request, *callback_args, **callback_kwargs)#012 File "/usr/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view#012 return view_func(*args, **kwargs)#012 File "/usr/lib/python2.7/site-packages/rest_framework/viewsets.py", line 90, in view#012 return self.dispatch(request, *args, **kwargs)#012 File "/usr/lib/python2.7/site-packages/rest_framework/views.py", line 489, in dispatch#012 response = self.handle_exception(exc)#012 File "/usr/lib/python2.7/site-packages/rest_framework/views.py", line 449, in handle_exception#012 self.raise_uncaught_exception(exc)#012 File "/usr/lib/python2.7/site-packages/rest_framework/views.py", line 486, in dispatch#012 response = handler(request, *args, **kwargs)#012 File "/usr/lib/python2.7/site-packages/rest_framework/mixins.py", line 20, in create#012 serializer.is_valid(raise_exception=True)#012 File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py", line 237, in is_valid#012 self._validated_data = self.run_validation(self.initial_data)#012 File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py", line 432, in run_validation#012 value = self.to_internal_value(data)#012 File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py", line 462, in to_internal_value#012 validated_value = field.run_validation(primitive_value)#012 File "/usr/lib/python2.7/site-packages/rest_framework/fields.py", line 524, in run_validation#012 value = self.to_internal_value(data)#012 File "/opt/netbox/netbox/extras/api/customfields.py", line 30, in to_internal_value#012 for field_name, value in data.items():#012AttributeError: 'unicode' object has no attribute 'items'` Here is the payload: `{ "name":"***********", "device_type":1772, "device_role":31, "tenant":null, "platform":null, "serial":"#########", "asset_tag":null, "site":16, "rack":1133, "position":25, "face":0, "status":1, "primary_ip4":null, "primary_ip6":null, "comments":"", "custom_fields":{"rack_tables_id":42} }`
adam closed this issue 2025-12-29 16:31:42 +01:00
Author
Owner

@4ndersson commented on GitHub (Oct 26, 2017):

I am getting almost the same when adding a device with custom_field.

Traceback:

Internal Server Error: /api/dcim/devices/
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/viewsets.py", line 90, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 489, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 449, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 486, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/mixins.py", line 20, in create
    serializer.is_valid(raise_exception=True)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 236, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 431, in run_validation
    value = self.to_internal_value(data)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 461, in to_internal_value
    validated_value = field.run_validation(primitive_value)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/fields.py", line 521, in run_validation
    value = self.to_internal_value(data)
  File "/opt/netbox/netbox/extras/api/customfields.py", line 30, in to_internal_value
    for field_name, value in data.items():
AttributeError: 'str' object has no attribute 'items'

Payload:

	data = {
		"site": 7,
		"platform": 1,
		"device_role": 6 ,
		"tenant": 166,
		"device_type": 25,
		"name": 'test6',
		"status": 1,
		"custom_fields": {"service_id":123456},
	}
@4ndersson commented on GitHub (Oct 26, 2017): I am getting almost the same when adding a device with custom_field. Traceback: ``` Internal Server Error: /api/dcim/devices/ Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 41, in inner response = get_response(request) File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view return view_func(*args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/rest_framework/viewsets.py", line 90, in view return self.dispatch(request, *args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 489, in dispatch response = self.handle_exception(exc) File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 449, in handle_exception self.raise_uncaught_exception(exc) File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 486, in dispatch response = handler(request, *args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/rest_framework/mixins.py", line 20, in create serializer.is_valid(raise_exception=True) File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 236, in is_valid self._validated_data = self.run_validation(self.initial_data) File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 431, in run_validation value = self.to_internal_value(data) File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 461, in to_internal_value validated_value = field.run_validation(primitive_value) File "/usr/local/lib/python3.5/dist-packages/rest_framework/fields.py", line 521, in run_validation value = self.to_internal_value(data) File "/opt/netbox/netbox/extras/api/customfields.py", line 30, in to_internal_value for field_name, value in data.items(): AttributeError: 'str' object has no attribute 'items' ``` Payload: ``` data = { "site": 7, "platform": 1, "device_role": 6 , "tenant": 166, "device_type": 25, "name": 'test6', "status": 1, "custom_fields": {"service_id":123456}, } ```
Author
Owner

@RyanBreaker commented on GitHub (Oct 26, 2017):

@kicker69101 When posting large blocks like you have, preface it with a line with ``` and end it with a line with ```. It'll then include the right formatting for lines and all for code and such and make it easier to read. 👍
See also: https://help.github.com/articles/creating-and-highlighting-code-blocks/

@RyanBreaker commented on GitHub (Oct 26, 2017): @kicker69101 When posting large blocks like you have, preface it with a line with ` ``` ` and end it with a line with ` ``` `. It'll then include the right formatting for lines and all for code and such and make it easier to read. 👍 See also: https://help.github.com/articles/creating-and-highlighting-code-blocks/
Author
Owner

@kicker69101 commented on GitHub (Oct 26, 2017):

@RyanBreaker Thanks, I'll will attempt to do this in future. However, in this case I pulled them from log files where they were already improperly formed.

@kicker69101 commented on GitHub (Oct 26, 2017): @RyanBreaker Thanks, I'll will attempt to do this in future. However, in this case I pulled them from log files where they were already improperly formed.
Author
Owner

@4ndersson commented on GitHub (Oct 27, 2017):

When the data is formed as "custom_fields": {"service_id":123456} ; It looks like /opt/netbox/netbox/extras/api/customfields.py only receives the string service_id (the whole dict and the value is stripped away somewhere) but the loops in the script expects a dict.

I did a few changes in customfields.py and is now only sending in "custom_fields" : 123456. Changes made in customfields.py (ugly, but it works for now):

line 30-40

        data_dict = {
        'service_id': ''
        }

        data_dict['service_id'] = data

        for field_name, value in data_dict.items():

line 130-140

        field_dict = {
        "service_id" : ''
        }

        field_dict['service_id'] = custom_fields

        for field_name, value in field_dict.items():
@4ndersson commented on GitHub (Oct 27, 2017): When the data is formed as `"custom_fields": {"service_id":123456}` ; It looks like _/opt/netbox/netbox/extras/api/customfields.py_ only receives the string **service_id** (the whole dict and the value is stripped away somewhere) but the loops in the script expects a dict. I did a few changes in customfields.py and is now only sending in `"custom_fields" : 123456`. Changes made in customfields.py (ugly, but it works for now): line 30-40 ``` data_dict = { 'service_id': '' } data_dict['service_id'] = data for field_name, value in data_dict.items(): ``` line 130-140 ``` field_dict = { "service_id" : '' } field_dict['service_id'] = custom_fields for field_name, value in field_dict.items(): ```
Author
Owner

@jeremystretch commented on GitHub (Oct 27, 2017):

@kicker69101 Please upgrade to the latest version of NetBox (v2.2.2) and try again. If you still experience this issue, please amend your post to include complete instructions for reproducing the bug, beginning with the creation of a custom field.

@jeremystretch commented on GitHub (Oct 27, 2017): @kicker69101 Please upgrade to the latest version of NetBox (v2.2.2) and try again. If you still experience this issue, please amend your post to include complete instructions for reproducing the bug, beginning with the creation of a custom field.
Author
Owner

@kicker69101 commented on GitHub (Oct 27, 2017):

Steps to reproduce:

  1. Create a custom field for devices named 'rack_tables_id' as an integer, but I get the same result as a string.
  2. Using rest api create a device that has the following payload:
{
"name":"***********",
"device_type":1772,
"device_role":31,
"tenant":null,
"platform":null,
"serial":"#########", 
"asset_tag":null, 
"site":16, 
"rack":1133, 
"position":25, 
"face":0, 
"status":1, 
"primary_ip4":null, 
"primary_ip6":null, 
"comments":"", 
"custom_fields":{
    "rack_tables_id":42
}
}
  1. I'm getting the following log entry (sorry for not formatting better, I'm getting this directly from a log file):
8502  139686073116480 django.request                                     ERROR    Internal Server Error: /api/dcim/devices/#012Traceback (most recent call last):#012  File "/usr/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner#012    response = get_response(request)#012  File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response#012    response = self.process_exception_by_middleware(e, request)#012  File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response#012    response = wrapped_callback(request, *callback_args, **callback_kwargs)#012  File "/usr/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view#012    return view_func(*args, **kwargs)#012  File "/usr/lib/python2.7/site-packages/rest_framework/viewsets.py", line 90, in view#012    return self.dispatch(request, *args, **kwargs)#012  File "/usr/lib/python2.7/site-packages/rest_framework/views.py", line 489, in dispatch#012    response = self.handle_exception(exc)#012  File "/usr/lib/python2.7/site-packages/rest_framework/views.py", line 449, in handle_exception#012    self.raise_uncaught_exception(exc)#012  File "/usr/lib/python2.7/site-packages/rest_framework/views.py", line 486, in dispatch#012    response = handler(request, *args, **kwargs)#012  File "/usr/lib/python2.7/site-packages/rest_framework/mixins.py", line 20, in create#012    serializer.is_valid(raise_exception=True)#012  File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py", line 236, in is_valid#012    self._validated_data = self.run_validation(self.initial_data)#012  File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py", line 431, in run_validation#012    value = self.to_internal_value(data)#012  File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py", line 461, in to_internal_value#012    validated_value = field.run_validation(primitive_value)#012  File "/usr/lib/python2.7/site-packages/rest_framework/fields.py", line 521, in run_validation#012    value = self.to_internal_value(data)#012  File "/opt/netbox/netbox/extras/api/customfields.py", line 30, in to_internal_value#012    for field_name, value in data.items():#012AttributeError: 'unicode' object has no attribute 'items'
@kicker69101 commented on GitHub (Oct 27, 2017): Steps to reproduce: 1. Create a custom field for devices named 'rack_tables_id' as an integer, but I get the same result as a string. 2. Using rest api create a device that has the following payload: ``` { "name":"***********", "device_type":1772, "device_role":31, "tenant":null, "platform":null, "serial":"#########", "asset_tag":null, "site":16, "rack":1133, "position":25, "face":0, "status":1, "primary_ip4":null, "primary_ip6":null, "comments":"", "custom_fields":{ "rack_tables_id":42 } } ``` 3. I'm getting the following log entry (sorry for not formatting better, I'm getting this directly from a log file): ``` 8502 139686073116480 django.request ERROR Internal Server Error: /api/dcim/devices/#012Traceback (most recent call last):#012 File "/usr/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner#012 response = get_response(request)#012 File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response#012 response = self.process_exception_by_middleware(e, request)#012 File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response#012 response = wrapped_callback(request, *callback_args, **callback_kwargs)#012 File "/usr/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view#012 return view_func(*args, **kwargs)#012 File "/usr/lib/python2.7/site-packages/rest_framework/viewsets.py", line 90, in view#012 return self.dispatch(request, *args, **kwargs)#012 File "/usr/lib/python2.7/site-packages/rest_framework/views.py", line 489, in dispatch#012 response = self.handle_exception(exc)#012 File "/usr/lib/python2.7/site-packages/rest_framework/views.py", line 449, in handle_exception#012 self.raise_uncaught_exception(exc)#012 File "/usr/lib/python2.7/site-packages/rest_framework/views.py", line 486, in dispatch#012 response = handler(request, *args, **kwargs)#012 File "/usr/lib/python2.7/site-packages/rest_framework/mixins.py", line 20, in create#012 serializer.is_valid(raise_exception=True)#012 File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py", line 236, in is_valid#012 self._validated_data = self.run_validation(self.initial_data)#012 File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py", line 431, in run_validation#012 value = self.to_internal_value(data)#012 File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py", line 461, in to_internal_value#012 validated_value = field.run_validation(primitive_value)#012 File "/usr/lib/python2.7/site-packages/rest_framework/fields.py", line 521, in run_validation#012 value = self.to_internal_value(data)#012 File "/opt/netbox/netbox/extras/api/customfields.py", line 30, in to_internal_value#012 for field_name, value in data.items():#012AttributeError: 'unicode' object has no attribute 'items' ```
Author
Owner

@jeremystretch commented on GitHub (Nov 2, 2017):

@kicker69101 I'm not able to reproduce this. Please try sending the request manually using curl (see the docs for examples) with the server running in debug mode and post the result. And again, make sure you're running the latest version of NetBox (v.2.2.4).

@jeremystretch commented on GitHub (Nov 2, 2017): @kicker69101 I'm not able to reproduce this. Please try sending the request manually using `curl` (see the docs for [examples](http://netbox.readthedocs.io/en/stable/api/examples/)) with the server running in debug mode and post the result. And again, make sure you're running the latest version of NetBox (v.2.2.4).
Author
Owner

@jeremystretch commented on GitHub (Nov 10, 2017):

Closing due to lack of activity.

@jeremystretch commented on GitHub (Nov 10, 2017): Closing due to lack of activity.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#1351