From 6e5d527fec33c7e94f20f8a2ae43626c29fda713 Mon Sep 17 00:00:00 2001 From: Brian Candler Date: Sat, 10 Aug 2019 10:04:13 +0100 Subject: [PATCH] Improve API error handling when a list is given as a choice value Fixes #3426 --- netbox/utilities/api.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/netbox/utilities/api.py b/netbox/utilities/api.py index 9354cbb30..9adfd84ad 100644 --- a/netbox/utilities/api.py +++ b/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):