Просмотр исходного кода

Merge pull request #3427 from candlerb/candlerb/3426

Improve API error handling when a list is given as a choice value
Jeremy Stretch 6 лет назад
Родитель
Сommit
aa858fea03
1 измененных файлов с 9 добавлено и 6 удалено
  1. 9 6
      netbox/utilities/api.py

+ 9 - 6
netbox/utilities/api.py

@@ -85,9 +85,9 @@ class ChoiceField(Field):
 
     def to_internal_value(self, data):
 
-        # Provide an explicit error message if the request is trying to write a dict
-        if type(data) is dict:
-            raise ValidationError('Value must be passed directly (e.g. "foo": 123); do not use a dictionary.')
+        # Provide an explicit error message if the request is trying to write a dict or list
+        if isinstance(data, (dict, list)):
+            raise ValidationError('Value must be passed directly (e.g. "foo": 123); do not use a dictionary or list.')
 
         # Check for string representations of boolean/integer values
         if hasattr(data, 'lower'):
@@ -101,10 +101,13 @@ class ChoiceField(Field):
                 except ValueError:
                     pass
 
-        if data not in self._choices:
-            raise ValidationError("{} is not a valid choice.".format(data))
+        try:
+            if data in self._choices:
+                return data
+        except TypeError:  # Input is an unhashable type
+            pass
 
-        return data
+        raise ValidationError("{} is not a valid choice.".format(data))
 
     @property
     def choices(self):