Fixes #2676: Fix exception when passing dictionary value to a ChoiceField

This commit is contained in:
Jeremy Stretch 2018-12-11 17:00:20 -05:00
parent b8a4316297
commit 8364e56e86
2 changed files with 19 additions and 9 deletions

View File

@ -3,6 +3,7 @@ v2.5.1 (FUTURE)
## Bug Fixes ## Bug Fixes
* [#2666](https://github.com/digitalocean/netbox/issues/2666) - Correct display of length unit in cables list * [#2666](https://github.com/digitalocean/netbox/issues/2666) - Correct display of length unit in cables list
* [#2676](https://github.com/digitalocean/netbox/issues/2676) - Fix exception when passing dictionary value to a ChoiceField
--- ---

View File

@ -78,17 +78,26 @@ class ChoiceField(Field):
return data return data
def to_internal_value(self, data): 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.')
# Check for string representations of boolean/integer values
if hasattr(data, 'lower'): if hasattr(data, 'lower'):
# Hotwiring boolean values from string
if data.lower() == 'true': if data.lower() == 'true':
return True data = True
if data.lower() == 'false': elif data.lower() == 'false':
return False data = False
# Check for string representation of an integer (e.g. "123") else:
try: try:
data = int(data) data = int(data)
except ValueError: except ValueError:
pass pass
if data not in self._choices:
raise ValidationError("{} is not a valid choice.".format(data))
return data return data