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

Closes #5003: CSV import now accepts slug values for choice fields

This commit is contained in:
Jeremy Stretch
2020-09-18 13:03:38 -04:00
parent 70ec5b9f37
commit 0cc2a6b2cf
5 changed files with 36 additions and 39 deletions

View File

@@ -117,18 +117,11 @@ class CSVChoiceField(forms.ChoiceField):
"""
Invert the provided set of choices to take the human-friendly label as input, and return the database value.
"""
def __init__(self, choices, *args, **kwargs):
super().__init__(choices=choices, *args, **kwargs)
self.choices = [(label, label) for value, label in unpack_grouped_choices(choices)]
self.choice_values = {label: value for value, label in unpack_grouped_choices(choices)}
STATIC_CHOICES = True
def clean(self, value):
value = super().clean(value)
if not value:
return ''
if value not in self.choice_values:
raise forms.ValidationError("Invalid choice: {}".format(value))
return self.choice_values[value]
def __init__(self, *, choices=(), **kwargs):
super().__init__(choices=choices, **kwargs)
self.choices = unpack_grouped_choices(choices)
class CSVModelChoiceField(forms.ModelChoiceField):