Allow ChoiceVar() to have optional setting #5877

Closed
opened 2025-12-29 19:33:44 +01:00 by adam · 1 comment
Owner

Originally created by @candlerb on GitHub (Jan 4, 2022).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.1.4

Feature type

Change to existing functionality

Proposed functionality

When you set required=False on a ChoiceVar, have no choice selected by default, and allow the user not to select anything.

Currently, if you have e.g.

cable_color = ChoiceVar(choices=ColorChoices.CHOICES, required=False, label="Cable Color")

then there is an arbitrary color chosen by default, and the user is required to select one.

Use case

For using built-in choices like CableTypeChoices.CHOICES or ColorChoices.CHOICES where no choice is a valid selection.

Workaround:

NO_CHOICE = (
        ('', '---------'),
)

cable_color = ChoiceVar(choices=NO_CHOICE+ColorChoices.CHOICES, required=False, label="Cable Color")

(Note however that required=False is still required)

Database changes

None

External dependencies

None

Originally created by @candlerb on GitHub (Jan 4, 2022). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.1.4 ### Feature type Change to existing functionality ### Proposed functionality When you set `required=False` on a ChoiceVar, have no choice selected by default, and allow the user not to select anything. Currently, if you have e.g. ``` cable_color = ChoiceVar(choices=ColorChoices.CHOICES, required=False, label="Cable Color") ``` then there is an arbitrary color chosen by default, and the user is required to select one. ### Use case For using built-in choices like `CableTypeChoices.CHOICES` or `ColorChoices.CHOICES` where no choice is a valid selection. Workaround: ``` NO_CHOICE = ( ('', '---------'), ) cable_color = ChoiceVar(choices=NO_CHOICE+ColorChoices.CHOICES, required=False, label="Cable Color") ``` (Note however that `required=False` *is* still required) ### Database changes None ### External dependencies None
adam added the type: bugstatus: accepted labels 2025-12-29 19:33:44 +01:00
adam closed this issue 2025-12-29 19:33:44 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 5, 2022):

Reclassifying this as a bug.

@jeremystretch commented on GitHub (Jan 5, 2022): Reclassifying this as a bug.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#5877