Unable to nullify choice fields via the API #3270

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

Originally created by @fantasmita on GitHub (Feb 4, 2020).

Originally assigned to: @jeremystretch on GitHub.

Environment

  • Python version: 3.6.5
  • NetBox version: 2.7.3

To fully unrack a device, you must blank out its rack, position, and face values.
The face field has a non-null constraint and has an API validator that requires its value to be either front or rear, so it's not possible to blank out the value through the API which prevents the device from being removed from the rack.

Steps to Reproduce

  1. Create a device in netbox with values for rack, position, and face.
  2. Try to clear all three values with a PATCH like the following
curl -X PATCH "https://$NETBOX/api/dcim/devices/$ID/" -H  "accept: application/json" -H  "Content-Type: application/json" -H  "X-CSRFToken: $TOKEN" -d "{  \"rack\": null,  \"position\": null,  \"face\": \"\"}"

Expected Behavior

The device to be unracked

Observed Behavior

{
  "face": [
    " is not a valid choice."
  ]
}

If passing null for the face value, the django non-null constraint is hit.
If patching just the rack and position values without touching the face, the Cannot select a rack face without assigning a rack. validator is hit.

Originally created by @fantasmita on GitHub (Feb 4, 2020). Originally assigned to: @jeremystretch on GitHub. <!-- NOTE: IF YOUR ISSUE DOES NOT FOLLOW THIS TEMPLATE, IT WILL BE CLOSED. This form is only for reproducible bugs. If you need assistance with NetBox installation, or if you have a general question, DO NOT open an issue. Instead, post to our mailing list: https://groups.google.com/forum/#!forum/netbox-discuss Please describe the environment in which you are running NetBox. Be sure that you are running an unmodified instance of the latest stable release before submitting a bug report. --> ### Environment * Python version: 3.6.5 * NetBox version: 2.7.3 <!-- Describe in detail the exact steps that someone else can take to reproduce this bug using the current stable release of NetBox (or the current beta release where applicable). Begin with the creation of any necessary database objects and call out every operation being performed explicitly. If reporting a bug in the REST API, be sure to reconstruct the raw HTTP request(s) being made: Don't rely on a wrapper like pynetbox. --> To fully unrack a device, you must blank out its rack, position, and face values. The `face` field has a non-null constraint and has an API validator that requires its value to be either `front` or `rear`, so it's not possible to blank out the value through the API which prevents the device from being removed from the rack. ### Steps to Reproduce 1. Create a device in netbox with values for rack, position, and face. 2. Try to clear all three values with a PATCH like the following ``` curl -X PATCH "https://$NETBOX/api/dcim/devices/$ID/" -H "accept: application/json" -H "Content-Type: application/json" -H "X-CSRFToken: $TOKEN" -d "{ \"rack\": null, \"position\": null, \"face\": \"\"}" ``` ### Expected Behavior The device to be unracked ### Observed Behavior ``` { "face": [ " is not a valid choice." ] } ``` If passing `null` for the face value, the django non-null constraint is hit. If patching just the `rack` and `position` values without touching the `face`, the `Cannot select a rack face without assigning a rack.` validator is hit.
adam added the type: bugstatus: accepted labels 2025-12-29 18:27:16 +01:00
adam closed this issue 2025-12-29 18:27:16 +01:00
Author
Owner

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

Omit the face field if you are unracking a device: It will be nullified automatically. The following request is successful:

curl -X PATCH http://localhost:8000/api/dcim/devices/<ID>/ \
-H "Authorization: Token <TOKEN>" \
-H "Content-Type: application/json" \
-H "Accept: application/json; indent=4" \
-d '{"rack": null, "position": null}'
@jeremystretch commented on GitHub (Feb 4, 2020): Omit the `face` field if you are unracking a device: It will be nullified automatically. The following request is successful: ``` curl -X PATCH http://localhost:8000/api/dcim/devices/<ID>/ \ -H "Authorization: Token <TOKEN>" \ -H "Content-Type: application/json" \ -H "Accept: application/json; indent=4" \ -d '{"rack": null, "position": null}' ```
Author
Owner

@fantasmita commented on GitHub (Feb 5, 2020):

Thanks for looking and for the suggestions.
However, I don't think the nullification is working as expected.

I know netboxdemo.com is unsanctioned, but here's a quick reproducible illustration of the problem in that environment when following your suggestions.

setting face to the empty string:

$ curl -X PATCH "https://netboxdemo.com/api/dcim/devices/1/" \
>     -H "Authorization: Token  72830d67beff4ae178b94d8f781842408df8069d" \
>     -H "Content-Type: application/json" \
>     -H "Accept: application/json; indent=4" \
>     -d '{"rack": null, "position": null, "face": ""}'
{
    "face": [
        " is not a valid choice."
    ]
}

omitting face:

$ curl -X PATCH "https://netboxdemo.com/api/dcim/devices/1/" \
>     -H "Authorization: Token  72830d67beff4ae178b94d8f781842408df8069d" \
>     -H "Content-Type: application/json" \
>     -H "Accept: application/json; indent=4" \
>     -d '{"rack": null, "position": null}'
{
    "face": [
        "Cannot select a rack face without assigning a rack."
    ]
}
@fantasmita commented on GitHub (Feb 5, 2020): Thanks for looking and for the suggestions. However, I don't think the nullification is working as expected. I know netboxdemo.com is unsanctioned, but here's a quick reproducible illustration of the problem in that environment when following your suggestions. setting `face` to the empty string: ``` $ curl -X PATCH "https://netboxdemo.com/api/dcim/devices/1/" \ > -H "Authorization: Token 72830d67beff4ae178b94d8f781842408df8069d" \ > -H "Content-Type: application/json" \ > -H "Accept: application/json; indent=4" \ > -d '{"rack": null, "position": null, "face": ""}' { "face": [ " is not a valid choice." ] } ``` omitting `face`: ``` $ curl -X PATCH "https://netboxdemo.com/api/dcim/devices/1/" \ > -H "Authorization: Token 72830d67beff4ae178b94d8f781842408df8069d" \ > -H "Content-Type: application/json" \ > -H "Accept: application/json; indent=4" \ > -d '{"rack": null, "position": null}' { "face": [ "Cannot select a rack face without assigning a rack." ] } ```
Author
Owner

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

Sorry, I had pasted the wrong command in my previous comment. I've corrected it to omit the face field. This is working on v2.7.4.

@jeremystretch commented on GitHub (Feb 5, 2020): Sorry, I had pasted the wrong command in my previous comment. I've corrected it to omit the `face` field. This is working on v2.7.4.
Author
Owner

@fantasmita commented on GitHub (Feb 5, 2020):

No worries, thanks.

I'm sorry for not explaining myself more clearly, but omitting the face when making a PATCH request doesn't successfully nullify the value before it hits the site / rack validators, which results in the Cannot select a rack face without assigning a rack. validation error I pasted above.
I confirmed today that this still occurs on a fresh installation of v2.7.4.

Anyway, I'm happy to work around this and it's definitely possible I'm missing something, but let me know if you have other thoughts or if I can provide any other information. Thanks again for your time

@fantasmita commented on GitHub (Feb 5, 2020): No worries, thanks. I'm sorry for not explaining myself more clearly, but omitting the `face` when making a PATCH request doesn't successfully nullify the value before it hits the site / rack validators, which results in the `Cannot select a rack face without assigning a rack.` validation error I pasted above. I confirmed today that this still occurs on a fresh installation of v2.7.4. Anyway, I'm happy to work around this and it's definitely possible I'm missing something, but let me know if you have other thoughts or if I can provide any other information. Thanks again for your time
Author
Owner

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

Sorry, this took a while to track down. I wasn't able to replicate the error earlier because the device that I was testing against did not have a rack face already assigned, so the validation check was not being triggered.

@jeremystretch commented on GitHub (Feb 10, 2020): Sorry, this took a while to track down. I wasn't able to replicate the error earlier because the device that I was testing against did not have a rack face already assigned, so the validation check was not being triggered.
Author
Owner

@fantasmita commented on GitHub (Feb 10, 2020):

Great, thanks a bunch!

@fantasmita commented on GitHub (Feb 10, 2020): Great, thanks a bunch!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3270