mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 09:51:22 -06:00
Fixes #2676: Fix exception when passing dictionary value to a ChoiceField
This commit is contained in:
parent
b8a4316297
commit
8364e56e86
@ -3,6 +3,7 @@ v2.5.1 (FUTURE)
|
||||
## Bug Fixes
|
||||
|
||||
* [#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
|
||||
|
||||
---
|
||||
|
||||
|
@ -78,17 +78,26 @@ class ChoiceField(Field):
|
||||
return 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'):
|
||||
# Hotwiring boolean values from string
|
||||
if data.lower() == 'true':
|
||||
return True
|
||||
if data.lower() == 'false':
|
||||
return False
|
||||
# Check for string representation of an integer (e.g. "123")
|
||||
try:
|
||||
data = int(data)
|
||||
except ValueError:
|
||||
pass
|
||||
data = True
|
||||
elif data.lower() == 'false':
|
||||
data = False
|
||||
else:
|
||||
try:
|
||||
data = int(data)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if data not in self._choices:
|
||||
raise ValidationError("{} is not a valid choice.".format(data))
|
||||
|
||||
return data
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user