1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Cleaned the CustomField choice field

This commit is contained in:
Saria Hajjar
2020-01-23 18:54:37 +00:00
parent 0a5eecd0e3
commit 8f86244b4f
2 changed files with 7 additions and 7 deletions

View File

@@ -442,18 +442,18 @@ class CSVChoiceField(forms.ChoiceField):
return self.choice_values[value]
class CSVCustomFieldChoiceField(forms.TypedChoiceField):
class CustomFieldChoiceField(forms.TypedChoiceField):
"""
Invert the provided set of choices to take the human-friendly label as input, and return the database value.
Accept human-friendly label as input, and return the database value. If the label is not matched, the normal,
value-based input is assumed.
"""
def __init__(self, choices, *args, **kwargs):
super().__init__(choices=choices, *args, **kwargs)
self.choice_values = {str(label): value for value, label in unpack_grouped_choices(choices)}
self.choice_values = {label: value for value, label in unpack_grouped_choices(choices)}
def clean(self, value):
if not value:
return None
# Check if the value is actually a label
if value in self.choice_values:
return self.choice_values[value]
return super().clean(value)