1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fixes #4083: Permit nullifying applicable choice fields via API requests

This commit is contained in:
Jeremy Stretch
2020-02-10 15:10:33 -05:00
parent a966a4c8ac
commit 7388fa3556
5 changed files with 41 additions and 15 deletions

View File

@ -61,10 +61,14 @@ class IsAuthenticatedOrLoginNotRequired(BasePermission):
class ChoiceField(Field):
"""
Represent a ChoiceField as {'value': <DB value>, 'label': <string>}.
Represent a ChoiceField as {'value': <DB value>, 'label': <string>}. Accepts a single value on write.
:param choices: An iterable of choices in the form (value, key).
:param allow_blank: Allow blank values in addition to the listed choices.
"""
def __init__(self, choices, **kwargs):
def __init__(self, choices, allow_blank=False, **kwargs):
self.choiceset = choices
self.allow_blank = allow_blank
self._choices = dict()
# Unpack grouped choices
@ -77,6 +81,15 @@ class ChoiceField(Field):
super().__init__(**kwargs)
def validate_empty_values(self, data):
# Convert null to an empty string unless allow_null == True
if data is None:
if self.allow_null:
return True, None
else:
data = ''
return super().validate_empty_values(data)
def to_representation(self, obj):
if obj is '':
return None
@ -93,6 +106,10 @@ class ChoiceField(Field):
return data
def to_internal_value(self, data):
if data is '':
if self.allow_blank:
return data
raise ValidationError("This field may not be blank.")
# Provide an explicit error message if the request is trying to write a dict or list
if isinstance(data, (dict, list)):