Exception when creating bulk devices using the API with a custom field. #3427

Closed
opened 2025-12-29 18:29:01 +01:00 by adam · 7 comments
Owner

Originally created by @ebusto on GitHub (Feb 28, 2020).

Originally assigned to: @jeremystretch on GitHub.

Environment

  • Python version: 3.7.6
  • NetBox version: 2.7.8

Steps to Reproduce

  1. Install NetBox 2.7.8. I used netbox-docker.
  2. Create an optional custom field on device objects.
  3. Add a device role, device type, and site.
  4. Attempt to create multiple devices in one API call, using either pynetbox or the interactive API docs, providing only the minimum necessary fields.

Expected Behavior

All devices to be created successfully.

Observed Behavior

...
File "/opt/netbox/netbox/extras/api/customfields.py" in __init__
  132.                 self.initial_data['custom_fields'] = {}
Exception Type: TypeError at /api/dcim/devices/
Exception Value: list indices must be integers or slices, not str
Originally created by @ebusto on GitHub (Feb 28, 2020). Originally assigned to: @jeremystretch on GitHub. ### Environment * Python version: 3.7.6 * NetBox version: 2.7.8 ### Steps to Reproduce 1. Install NetBox 2.7.8. I used [netbox-docker](https://github.com/netbox-community/netbox-docker). 2. Create an optional custom field on device objects. 3. Add a device role, device type, and site. 4. Attempt to create multiple devices in one API call, using either pynetbox or the interactive API docs, providing only the minimum necessary fields. ### Expected Behavior All devices to be created successfully. ### Observed Behavior ``` ... File "/opt/netbox/netbox/extras/api/customfields.py" in __init__ 132. self.initial_data['custom_fields'] = {} Exception Type: TypeError at /api/dcim/devices/ Exception Value: list indices must be integers or slices, not str ```
adam added the type: bugstatus: accepted labels 2025-12-29 18:29:01 +01:00
adam closed this issue 2025-12-29 18:29:01 +01:00
Author
Owner

@jeremystretch commented on GitHub (Feb 28, 2020):

Attempt to create multiple devices in one API call

Please provide the exact API call needed to recreate the issue (using curl).

@jeremystretch commented on GitHub (Feb 28, 2020): > Attempt to create multiple devices in one API call Please provide the exact API call needed to recreate the issue (using `curl`).
Author
Owner

@ebusto commented on GitHub (Feb 28, 2020):

curl -X POST -H "Authorization: Token ${TOKEN}" -H "Content-Type: application/json" -d '[{"device_type": 1, "device_role": 1, "site": 1}]' http://0.0.0.0:32768/api/dcim/devices/
@ebusto commented on GitHub (Feb 28, 2020): ``` curl -X POST -H "Authorization: Token ${TOKEN}" -H "Content-Type: application/json" -d '[{"device_type": 1, "device_role": 1, "site": 1}]' http://0.0.0.0:32768/api/dcim/devices/ ```
Author
Owner

@DanSheps commented on GitHub (Mar 2, 2020):

curl -X POST "https://master.netbox.dansheps.com/api/dcim/devices/" -H "accept: application/json" -H "Content-Type: application/json" -d "[{ \"device_type\": 1, \"device_role\": 1, \"site\": 2},{ \"device_type\": 1, \"device_role\": 1, \"site\": 2}]"

[
  {
    "id": 5,
...
  },
  {
    "id": 6,
...
  }
]

Could you try against master.netbox.dansheps.com please? I am not seeing any issues

@DanSheps commented on GitHub (Mar 2, 2020): ``` curl -X POST "https://master.netbox.dansheps.com/api/dcim/devices/" -H "accept: application/json" -H "Content-Type: application/json" -d "[{ \"device_type\": 1, \"device_role\": 1, \"site\": 2},{ \"device_type\": 1, \"device_role\": 1, \"site\": 2}]" [ { "id": 5, ... }, { "id": 6, ... } ] ``` Could you try against master.netbox.dansheps.com please? I am not seeing any issues
Author
Owner

@ebusto commented on GitHub (Mar 3, 2020):

Hi @DanSheps, I think you forgot to create a custom field. Once I created a custom field on your instance, I was able to reproduce the error against master.netbox.dansheps.com.

mana:~ ebusto$ curl -X POST "https://master.netbox.dansheps.com/api/dcim/devices/" -H "accept: application/json" -H "Content-Type: application/json" -d "[{ \"device_type\": 1, \"device_role\": 1, \"site\": 2},{ \"device_type\": 1, \"device_role\": 1, \"site\": 2}]" -H "Authorization: Token <token>"

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Server Error</title>
    <link rel="stylesheet" href="/static/bootstrap-3.4.1-dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.min.css">
    <meta charset="UTF-8">
</head>
...
@ebusto commented on GitHub (Mar 3, 2020): Hi @DanSheps, I think you forgot to create a custom field. Once I created a custom field on your instance, I was able to reproduce the error against `master.netbox.dansheps.com`. ``` mana:~ ebusto$ curl -X POST "https://master.netbox.dansheps.com/api/dcim/devices/" -H "accept: application/json" -H "Content-Type: application/json" -d "[{ \"device_type\": 1, \"device_role\": 1, \"site\": 2},{ \"device_type\": 1, \"device_role\": 1, \"site\": 2}]" -H "Authorization: Token <token>" <!DOCTYPE html> <html lang="en"> <head> <title>Server Error</title> <link rel="stylesheet" href="/static/bootstrap-3.4.1-dist/css/bootstrap.min.css"> <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.min.css"> <meta charset="UTF-8"> </head> ... ```
Author
Owner

@DanSheps commented on GitHub (Mar 3, 2020):

Yeah, that was my bad, I thought there were some custom fields created already.

@DanSheps commented on GitHub (Mar 3, 2020): Yeah, that was my bad, I thought there were some custom fields created already.
Author
Owner

@kobayashi commented on GitHub (Mar 3, 2020):

I can reproduce this issue with custom fields.
This is caused at the following line because self.initial_data is expected as dict not list.
7b6bd75c22/netbox/extras/api/customfields.py (L130-L132)

It is trying to set custom_fields not to each device object but to list of them.

Does NetBox support bulk import through /api/dcim/devices?
If so, we need to modify CustomFieldModelSerializer to support this. Or may be much better way.

@kobayashi commented on GitHub (Mar 3, 2020): I can reproduce this issue with custom fields. This is caused at the following line because `self.initial_data` is expected as dict not list. https://github.com/netbox-community/netbox/blob/7b6bd75c22196a28b6aa241b4146c6000ff3805c/netbox/extras/api/customfields.py#L130-L132 It is trying to set `custom_fields` not to each device object but to list of them. Does NetBox support bulk import through `/api/dcim/devices`? If so, we need to modify `CustomFieldModelSerializer` to support this. Or may be much better way.
Author
Owner

@jeremystretch commented on GitHub (Mar 3, 2020):

This code was written a long time ago, before we implemented the ability to create multiple items in bulk via the REST API. It likely needs a substantial amount of refactoring. I'll dig into this some more.

@jeremystretch commented on GitHub (Mar 3, 2020): This code was written a long time ago, before we implemented the ability to create multiple items in bulk via the REST API. It likely needs a substantial amount of refactoring. I'll dig into this some more.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3427