Selaa lähdekoodia

Cleaned the CustomField choice field

Saria Hajjar 6 vuotta sitten
vanhempi
commit
8f86244b4f
2 muutettua tiedostoa jossa 7 lisäystä ja 7 poistoa
  1. 2 2
      netbox/extras/forms.py
  2. 5 5
      netbox/utilities/forms.py

+ 2 - 2
netbox/extras/forms.py

@@ -10,7 +10,7 @@ from dcim.models import DeviceRole, Platform, Region, Site
 from tenancy.models import Tenant, TenantGroup
 from utilities.forms import (
     add_blank_choice, APISelectMultiple, BootstrapMixin, BulkEditForm, BulkEditNullBooleanSelect, ColorSelect,
-    CSVCustomFieldChoiceField, CommentField, ContentTypeSelect, DatePicker, DateTimePicker, FilterChoiceField,
+    CustomFieldChoiceField, CommentField, ContentTypeSelect, DatePicker, DateTimePicker, FilterChoiceField,
     LaxURLField, JSONField, SlugField, StaticSelect2, BOOLEAN_WITH_BLANK_CHOICES,
 )
 from .choices import *
@@ -71,7 +71,7 @@ def get_custom_fields_for_model(content_type, filterable_only=False, bulk_edit=F
                     default_choice = cf.choices.get(value=initial).pk
                 except ObjectDoesNotExist:
                     pass
-            field = CSVCustomFieldChoiceField(
+            field = CustomFieldChoiceField(
                 choices=choices, coerce=int, required=cf.required, initial=default_choice, widget=StaticSelect2()
             )
 

+ 5 - 5
netbox/utilities/forms.py

@@ -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)