Values for Selection Custom Fields via API #1472

Closed
opened 2025-12-29 16:32:21 +01:00 by adam · 11 comments
Owner

Originally created by @cimnine on GitHub (Dec 22, 2017).

Issue type

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

Environment

  • Python version: 3.6
  • NetBox version: 2.2.8

Description

As I already told on the mailing list, I would like to fill in the values of selection custom fields via the API.

I expected to provide the actual value of the field, i.e. the text I see in the dropdown of the GUI. But the API expects to get the ID of the option, rather than the value, and there seems to be no way of looking up the available (value,id)-pairs for a given selection custom fields. Also I would like to avoid hardcoding any (value,id)-pairs in my application, as it's not consistent across our different Netbox instances (local, staging, production) and it would be very fragile.

I see two solutions:

  • When I set the value of such a selection custom field, Netbox accepts the String value and converts it to the respective ID internally.
  • There is a new endpoint to fetch the option pairs of selection custom fields, similar to the other _options endpoints.

IMO both options could even live in parallel to each other: Whenever an integer is presented as value for a selection custom field, we can assume it's the ID of the value. Whenever it's a string, we can assume it's the text value and first has to be converted to the corresponding integer id.

Allowing to submit text values via the API instead of the actual IDs might imply that we don't allow the same text value on one given selection custom field twice.

Originally created by @cimnine on GitHub (Dec 22, 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. For assistance with installation issues, or for any other issues other than those listed below, please raise your topic 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. Due to an excessive backlog of feature requests, we are not currently accepting any proposals which extend NetBox's feature scope. Do not prepend any sort of tag to your issue's title. An administrator will review your issue and assign labels as appropriate. ---> ### Issue type [X] Feature request <!-- An enhancement of existing functionality --> [ ] Bug report <!-- Unexpected or erroneous behavior --> [ ] Documentation <!-- 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.) If you are submitting a bug report and have made any changes to the code base, please first validate that your bug can be recreated while running an official release. --> ### Environment * Python version: 3.6 * NetBox version: 2.2.8 <!-- BUG REPORTS must include: * A list of the steps needed for someone else to reproduce the bug * A description of the expected and observed 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 As I [already told on the mailing list](https://groups.google.com/forum/#!topic/netbox-discuss/4j7ODlKWNk0), I would like to fill in the values of _selection custom fields_ via the API. I expected to provide the actual value of the field, i.e. the text I see in the dropdown of the GUI. But the API expects to get the ID of the option, rather than the value, and there seems to be no way of looking up the available (value,id)-pairs for a given _selection custom fields_. Also I would like to avoid hardcoding any (value,id)-pairs in my application, as it's not consistent across our different Netbox instances (local, staging, production) and it would be very fragile. I see two solutions: * When I set the value of such a selection custom field, Netbox accepts the String value and converts it to the respective ID internally. * There is a new endpoint to fetch the option pairs of selection custom fields, similar to the other __options_ endpoints. IMO both options could even live in parallel to each other: Whenever an integer is presented as value for a selection custom field, we can assume it's the ID of the value. Whenever it's a string, we can assume it's the text value and first has to be converted to the corresponding integer id. Allowing to submit text values via the API instead of the actual IDs might imply that we don't allow the same text value on one given selection custom field twice.
adam added the status: acceptedtype: feature labels 2025-12-29 16:32:21 +01:00
adam closed this issue 2025-12-29 16:32:21 +01:00
Author
Owner

@cimnine commented on GitHub (Jan 19, 2018):

I just learned that the behaviour I described was already discussed in https://github.com/digitalocean/netbox/issues/1297, yet no solution was proposed / implemented to get the primary keys of values via the API.

Which would be important when the selection options change over time or when the PKs differ between independant instances of Netbox (e.g. staging/production).

@cimnine commented on GitHub (Jan 19, 2018): I just learned that the behaviour I described was already discussed in https://github.com/digitalocean/netbox/issues/1297, yet no solution was proposed / implemented to get the primary keys of values via the API. Which would be important when the selection options change over time or when the PKs differ between independant instances of Netbox (e.g. staging/production).
Author
Owner

@arionl commented on GitHub (May 8, 2018):

I ran into this same issue when trying to update a selection-based custom field I'm using through APIs (and pynetbox). I'm really trying to avoid custom fields whenever I can but I still need a few. In general I'd rather have a custom field with fixed selection options rather than a string to try to limit data diversity.. but without a programmatic way of changing selection values, it's getting a bit challenging..

@arionl commented on GitHub (May 8, 2018): I ran into this same issue when trying to update a selection-based custom field I'm using through APIs (and [pynetbox](https://github.com/digitalocean/pynetbox)). I'm really trying to avoid custom fields whenever I can but I still need a few. In general I'd rather have a custom field with fixed selection options rather than a string to try to limit data diversity.. but without a programmatic way of changing selection values, it's getting a bit challenging..
Author
Owner

@jeremystretch commented on GitHub (Jul 2, 2018):

We need to decide how to expose the custom field choices in the API. Currently, each app has a _choices endpoint which returns the choices for all models in the form "model:field": [choices]:

{
    "device:face": [
        {
            "label": "Front",
            "value": 0
        },
        {
            "label": "Rear",
            "value": 1
        }
    ],
    "device:status": [
        {
            "label": "Active",
            "value": 1
        },
        ...
    ]
}

We can't do model:custom_field_name because it's possible that custom_field_name will collide with the name of a built-in field. We need a way to distinguish custom fields.

One option would be to prepend cf_ to the field name, such that custom field foo on the Device model would become device:cf_foo, but that may not be the cleanest approach. Open to suggestions.

@jeremystretch commented on GitHub (Jul 2, 2018): We need to decide how to expose the custom field choices in the API. Currently, each app has a `_choices` endpoint which returns the choices for all models in the form `"model:field": [choices]`: ``` { "device:face": [ { "label": "Front", "value": 0 }, { "label": "Rear", "value": 1 } ], "device:status": [ { "label": "Active", "value": 1 }, ... ] } ``` We can't do `model:custom_field_name` because it's possible that `custom_field_name` will collide with the name of a built-in field. We need a way to distinguish custom fields. One option would be to prepend `cf_` to the field name, such that custom field `foo` on the Device model would become `device:cf_foo`, but that may not be the cleanest approach. Open to suggestions.
Author
Owner

@a31amit commented on GitHub (Jul 9, 2018):

I am also very much interested in this feature.

@a31amit commented on GitHub (Jul 9, 2018): I am also very much interested in this feature.
Author
Owner

@lampwins commented on GitHub (Jul 9, 2018):

I feel this should be its own API endpoint. Perhaps /extras/custom-fields/_choices/. This way it can act just like the built-in app choices endpoints, but there can be no name collision or name manipulation. It is also very apparent that these choices apply only to custom fields.

@lampwins commented on GitHub (Jul 9, 2018): I feel this should be its own API endpoint. Perhaps `/extras/custom-fields/_choices/`. This way it can act just like the built-in app choices endpoints, but there can be no name collision or name manipulation. It is also very apparent that these choices apply only to custom fields.
Author
Owner

@jasweet commented on GitHub (Sep 24, 2018):

@jeremystretch I'd be fine with any way to lookup the '_choices' label using the value. Doesn't have to be pretty, just has to work.

@jasweet commented on GitHub (Sep 24, 2018): @jeremystretch I'd be fine with any way to lookup the '_choices' label using the value. Doesn't have to be pretty, just has to work.
Author
Owner

@Grokzen commented on GitHub (Jan 9, 2019):

@jeremystretch If issue would be accepted, i got the following branch ready to be merged https://github.com/Grokzen/netbox/compare/develop...Grokzen:feature/custom_field_choice_api_endpoint

It would add a new API endpoint /api/extras/_custom_field_choices/ and it would output in the following format

{
  "archiving.retention": {
    "Archiving 13 Months": 493,
    "Archiving 7 Years": 494,
    "Archiving 10 Years": 495,
    "Other": 496
  },
  "backup.database.dbfrequency": {
    "1 per day": 515,
    "1 per hour": 516
  },
}

External services can query all fields and cache them locally for a period of time, or they can look for a specific value each time and pass it into the next api call to POST an item.

@Grokzen commented on GitHub (Jan 9, 2019): @jeremystretch If issue would be accepted, i got the following branch ready to be merged https://github.com/Grokzen/netbox/compare/develop...Grokzen:feature/custom_field_choice_api_endpoint It would add a new API endpoint `/api/extras/_custom_field_choices/` and it would output in the following format ``` { "archiving.retention": { "Archiving 13 Months": 493, "Archiving 7 Years": 494, "Archiving 10 Years": 495, "Other": 496 }, "backup.database.dbfrequency": { "1 per day": 515, "1 per hour": 516 }, } ``` External services can query all fields and cache them locally for a period of time, or they can look for a specific value each time and pass it into the next api call to POST an item.
Author
Owner

@jeremystretch commented on GitHub (Feb 22, 2019):

@Grokzen I think that looks good if you want to submit a PR. I like @lampwins' idea of putting the endpoint at /extras/custom-fields/_choices/, but that wouldn't show up in the browsable API, so your way is probably preferable to make it more obvious.

@jeremystretch commented on GitHub (Feb 22, 2019): @Grokzen I think that looks good if you want to submit a PR. I like @lampwins' idea of putting the endpoint at `/extras/custom-fields/_choices/`, but that wouldn't show up in the browsable API, so your way is probably preferable to make it more obvious.
Author
Owner

@Grokzen commented on GitHub (Feb 24, 2019):

Awesome, i will put it up during the upcomming workweek

@Grokzen commented on GitHub (Feb 24, 2019): Awesome, i will put it up during the upcomming workweek
Author
Owner

@jeremystretch commented on GitHub (Mar 4, 2019):

Closed in #2941. Thanks @Grokzen!

@jeremystretch commented on GitHub (Mar 4, 2019): Closed in #2941. Thanks @Grokzen!
Author
Owner

@jasweet commented on GitHub (Mar 4, 2019):

@Grokzen & @jeremystretch
Great work! This is fan-friggin-tastic, and a much needed endpoint function.

@jasweet commented on GitHub (Mar 4, 2019): @Grokzen & @jeremystretch Great work! This is fan-friggin-tastic, and a much needed endpoint function.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#1472