mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 17:38:37 -06:00
Rename and simplify CustomFieldChoiceField
This commit is contained in:
parent
bc7cf63958
commit
f12199dcb5
@ -442,21 +442,18 @@ class CSVChoiceField(forms.ChoiceField):
|
|||||||
return self.choice_values[value]
|
return self.choice_values[value]
|
||||||
|
|
||||||
|
|
||||||
class CustomFieldChoiceField(forms.TypedChoiceField):
|
class CSVCustomFieldChoiceField(forms.TypedChoiceField):
|
||||||
"""
|
"""
|
||||||
Accept human-friendly label as input, and return the database value. If the label is not matched, the normal,
|
Invert the choice tuples: CSV import takes the human-friendly label as input rather than the database value
|
||||||
value-based input is assumed.
|
|
||||||
"""
|
"""
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
|
||||||
def __init__(self, choices, *args, **kwargs):
|
if 'choices' in kwargs:
|
||||||
super().__init__(choices=choices, *args, **kwargs)
|
kwargs['choices'] = {
|
||||||
self.choice_values = {label: value for value, label in unpack_grouped_choices(choices)}
|
label: value for value, label in kwargs['choices']
|
||||||
|
}
|
||||||
|
|
||||||
def clean(self, value):
|
super().__init__(*args, **kwargs)
|
||||||
# Check if the value is actually a label
|
|
||||||
if value in self.choice_values:
|
|
||||||
return self.choice_values[value]
|
|
||||||
return super().clean(value)
|
|
||||||
|
|
||||||
|
|
||||||
class ExpandableNameField(forms.CharField):
|
class ExpandableNameField(forms.CharField):
|
||||||
|
Loading…
Reference in New Issue
Block a user