Custom field with trailing white space unable to import #9211

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

Originally created by @ponchofiesta on GitHub (Feb 8, 2024).

Originally assigned to: @Julio-Oliveira-Encora on GitHub.

Deployment Type

Self-hosted

NetBox Version

v3.6.9

Python Version

3.8

Steps to Reproduce

  1. Create custom field for Virtual machine with multi selection
  2. One value has a trailing whitespace, e.g. "bla "
  3. Try to import this value via CSV:
id,cf_CustField
123,bla

This won't work too:

id,cf_CustField
123,"bla "

But this works:

id,cf_CustField
123,"bla ,anotherBla"

Expected Behavior

It will get imported

Suggestion: By default remove any leading and trailing white spaces form field values.

Observed Behavior

Error: "Record 1 cf_CustField: Select a valid choice. bla is not one of the available choices."

Originally created by @ponchofiesta on GitHub (Feb 8, 2024). Originally assigned to: @Julio-Oliveira-Encora on GitHub. ### Deployment Type Self-hosted ### NetBox Version v3.6.9 ### Python Version 3.8 ### Steps to Reproduce 1. Create custom field for Virtual machine with multi selection 2. One value has a trailing whitespace, e.g. "bla " 3. Try to import this value via CSV: ```csv id,cf_CustField 123,bla ``` This won't work too: ```csv id,cf_CustField 123,"bla " ``` But this works: ```csv id,cf_CustField 123,"bla ,anotherBla" ``` ### Expected Behavior It will get imported Suggestion: By default remove any leading and trailing white spaces form field values. ### Observed Behavior Error: "Record 1 cf_CustField: Select a valid choice. bla is not one of the available choices."
adam added the type: bugstatus: acceptedseverity: low labels 2025-12-29 20:47:06 +01:00
adam closed this issue 2025-12-29 20:47:06 +01:00
Author
Owner

@jeremystretch commented on GitHub (Feb 8, 2024):

Thank you for opening a bug report. I was unable to reproduce the reported behavior on NetBox v3.7.1. Please re-confirm the reported behavior on the current stable release and adjust your post above as necessary. Remember to provide detailed steps that someone else can follow using a clean installation of NetBox to reproduce the issue. Remember to include the steps taken to create any initial objects or other data.

@jeremystretch commented on GitHub (Feb 8, 2024): Thank you for opening a bug report. I was unable to reproduce the reported behavior on NetBox v3.7.1. Please re-confirm the reported behavior on the current stable release and adjust your post above as necessary. Remember to provide detailed steps that someone else can follow using a clean installation of NetBox to reproduce the issue. Remember to include the steps taken to create any initial objects or other data.
Author
Owner

@sleepinggenius2 commented on GitHub (Feb 8, 2024):

I would be extremely careful about automatically stripping leading and trailing white space in imports. There is already a pretty frustrating bug when importing tab-separated data where the input gets trimmed and it breaks on the last row if it has legitimate trailing tabs for empty columns. I think the expectation should be that data entered during a bulk import has already been adequately cleaned beforehand.

@sleepinggenius2 commented on GitHub (Feb 8, 2024): I would be extremely careful about automatically stripping leading and trailing white space in imports. There is already a pretty frustrating bug when importing tab-separated data where the input gets trimmed and it breaks on the last row if it has legitimate trailing tabs for empty columns. I think the expectation should be that data entered during a bulk import has already been adequately cleaned beforehand.
Author
Owner

@ponchofiesta commented on GitHub (Feb 8, 2024):

I'm not sure if the problem was understand correctly. Wgen defining the custom field, one of the possible values has a trailung blank. And it is not possbile to import this value using CSV import. It looks like Netbox already drops the blank on an import and therefore the importvalue doesn't match the custom field value.

@ponchofiesta commented on GitHub (Feb 8, 2024): I'm not sure if the problem was understand correctly. Wgen defining the custom field, one of the possible values has a trailung blank. And it is not possbile to import this value using CSV import. It looks like Netbox already drops the blank on an import and therefore the importvalue doesn't match the custom field value.
Author
Owner

@ponchofiesta commented on GitHub (Feb 9, 2024):

I analyzed the source code a little and I think I found the cause. The csv reader doesn't drop the blank values but Netbox does:
https://github.com/netbox-community/netbox/blob/develop/netbox/utilities/forms/utils.py#L237

And if I understand the code correctly, the CustomFieldChoices are not striped.

@ponchofiesta commented on GitHub (Feb 9, 2024): I analyzed the source code a little and I think I found the cause. The csv reader doesn't drop the blank values but Netbox does: https://github.com/netbox-community/netbox/blob/develop/netbox/utilities/forms/utils.py#L237 And if I understand the code correctly, the CustomFieldChoices are not striped.
Author
Owner

@sleepinggenius2 commented on GitHub (Feb 9, 2024):

Suggestion: By default remove any leading and trailing white spaces form field values.

That made it sound like you were saying that the fields should be stripped in the CSV import, not on the custom field choice value. It sounds though like the CSV fields are being stripped, while the custom field choice values are not. So, is this bug actually for the custom field choice values not being stripped? If so, that was extremely unclear from the initial bug report, but it does make sense to me.

That being said, I also find it kind of surprising and a bit unintuitive from a UX standpoint that a quoted value in the CSV import would still strip white space. I might understand that if the field was just left raw and unquoted, but maybe that nuance of unquoting is handled by the CSV library and the NetBox code is not able to make that distinction.

@sleepinggenius2 commented on GitHub (Feb 9, 2024): > Suggestion: By default remove any leading and trailing white spaces form field values. That made it sound like you were saying that the fields should be stripped in the CSV import, not on the custom field choice value. It sounds though like the CSV fields are being stripped, while the custom field choice values are not. So, is this bug actually for the custom field choice values not being stripped? If so, that was extremely unclear from the initial bug report, but it does make sense to me. That being said, I also find it kind of surprising and a bit unintuitive from a UX standpoint that a quoted value in the CSV import would still strip white space. I might understand that if the field was just left raw and unquoted, but maybe that nuance of unquoting is handled by the CSV library and the NetBox code is not able to make that distinction.
Author
Owner

@ponchofiesta commented on GitHub (Feb 23, 2024):

@jeremystretch I verified it with current Netbox version using Docker [v3.7.3-2.8.0].

Steps:

  • Created Super user, Dummy Cluster, VM, Custom field
  • Create Custom Field Choice Set with these exact "Extra Choices" (note the " " after "Space"):
    Space 
    NoSpace
    AnotherOne
    
  • Create the Custom Field for VMs using the Custom Field Choice Set
  • Try to import this data to the VM via CSV. All of these variants will fail:
    Without extra space:
    id,cf_TestField
    1,Space
    
    With extra space after "Space":
    id,cf_TestField
    1,Space 
    
    With extra space and quotes:
    id,cf_TestField
    1,"Space "
    

And this happens because Netbox trims import data fields but not field choice values. I'd say trimming import data is incorrect behavior.

@ponchofiesta commented on GitHub (Feb 23, 2024): @jeremystretch I verified it with current Netbox version using Docker [v3.7.3-2.8.0]. Steps: - Created Super user, Dummy Cluster, VM, Custom field - Create Custom Field Choice Set with these exact "Extra Choices" (note the " " after "Space"): ``` Space NoSpace AnotherOne ``` - Create the Custom Field for VMs using the Custom Field Choice Set - Try to import this data to the VM via CSV. All of these variants will fail: Without extra space: ``` id,cf_TestField 1,Space ``` With extra space after "Space": ``` id,cf_TestField 1,Space ``` With extra space and quotes: ``` id,cf_TestField 1,"Space " ``` And this happens because Netbox trims import data fields but not field choice values. I'd say trimming import data is incorrect behavior.
Author
Owner

@Julio-Oliveira-Encora commented on GitHub (May 17, 2024):

Could you please assign it to me?

@Julio-Oliveira-Encora commented on GitHub (May 17, 2024): Could you please assign it to me?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9211