Unable to create custom field of type object or multiobject via API #6263

Closed
opened 2025-12-29 19:38:41 +01:00 by adam · 2 comments
Owner

Originally created by @squintfox on GitHub (Mar 26, 2022).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.2-beta2

Python version

3.9

Steps to Reproduce

  1. POST the following to: https:///api/extras/custom-fields/
    {
    "content_types": [
    "ipam.prefix"
    ],
    "type": "object",
    "data_type": "object",
    "object_type": "circuits.circuit",
    "name": "circuit",
    "label": "Circuit"
    }

Expected Behavior

A new custom field should be created.

Observed Behavior

The API returns:
{
"object_type": [
"Object fields must define an object type."
]
}

It does not matter what value you supply for object_type or if you don't specify one at all.

Originally created by @squintfox on GitHub (Mar 26, 2022). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.2-beta2 ### Python version 3.9 ### Steps to Reproduce 1. POST the following to: https://<host>/api/extras/custom-fields/ { "content_types": [ "ipam.prefix" ], "type": "object", "data_type": "object", "object_type": "circuits.circuit", "name": "circuit", "label": "Circuit" } ### Expected Behavior A new custom field should be created. ### Observed Behavior The API returns: { "object_type": [ "Object fields must define an object type." ] } It does not matter what value you supply for object_type or if you don't specify one at all.
adam added the type: bugstatus: acceptedbeta labels 2025-12-29 19:38:41 +01:00
adam closed this issue 2025-12-29 19:38:42 +01:00
Author
Owner

@squintfox commented on GitHub (Mar 26, 2022):

I believe (at least in part) the issue is the the field object_type is missing from CustomFieldSerializer in /netbox/netbox/extras/api/serializers.py. I modified this:

fields = [
'id', 'url', 'display', 'content_types', 'type', 'data_type', 'name', 'label', 'description', 'required',
'filter_logic', 'default', 'weight', 'validation_minimum', 'validation_maximum', 'validation_regex',
'object_type', 'choices', 'created', 'last_updated',
]

After I restarted netbox, object_type was now shown in API GET queries for custom fields (it was not previously) and I was able to create a custom field by supplying the int id of "circuits.circuit". Given what I've see of the API and how it handles linked objects, this doesn't seem like a complete solution, but hopefully this is helpful for someone who understands it better than I do.

@squintfox commented on GitHub (Mar 26, 2022): I believe (at least in part) the issue is the the field object_type is missing from CustomFieldSerializer in /netbox/netbox/extras/api/serializers.py. I modified this: fields = [ 'id', 'url', 'display', 'content_types', 'type', 'data_type', 'name', 'label', 'description', 'required', 'filter_logic', 'default', 'weight', 'validation_minimum', 'validation_maximum', 'validation_regex', **'object_type',** 'choices', 'created', 'last_updated', ] After I restarted netbox, object_type was now shown in API GET queries for custom fields (it was not previously) and I was able to create a custom field by supplying the int id of "circuits.circuit". Given what I've see of the API and how it handles linked objects, this doesn't seem like a complete solution, but hopefully this is helpful for someone who understands it better than I do.
Author
Owner

@jeremystretch commented on GitHub (Mar 28, 2022):

@squintfox yep, that's pretty much it. In this case, we want to make object_type a ContentTypeField, so that it returns a human-friendly descriptor (e.g. "circuit.circuit") instead of the PK, adding the missing field was the key. Thanks for the report!

@jeremystretch commented on GitHub (Mar 28, 2022): @squintfox yep, that's pretty much it. In this case, we want to make `object_type` a ContentTypeField, so that it returns a human-friendly descriptor (e.g. `"circuit.circuit"`) instead of the PK, adding the missing field was the key. Thanks for the report!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6263