mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Tweaked ChoiceFieldSerializer to display a field as (value, label)
This commit is contained in:
netbox
@ -103,7 +103,7 @@ class RackSerializer(CustomFieldModelSerializer):
|
||||
tenant = NestedTenantSerializer()
|
||||
role = NestedRackRoleSerializer()
|
||||
type = ChoiceFieldSerializer(choices=RACK_TYPE_CHOICES)
|
||||
# width = ChoiceFieldSerializer(choices=RACK_WIDTH_CHOICES)
|
||||
width = ChoiceFieldSerializer(choices=RACK_WIDTH_CHOICES)
|
||||
|
||||
class Meta:
|
||||
model = Rack
|
||||
|
@ -12,18 +12,18 @@ class ServiceUnavailable(APIException):
|
||||
|
||||
class ChoiceFieldSerializer(Field):
|
||||
"""
|
||||
Represent a ChoiceField as a list of (value, label) tuples.
|
||||
Represent a ChoiceField as (value, label).
|
||||
"""
|
||||
|
||||
def __init__(self, choices, **kwargs):
|
||||
self._choices = choices
|
||||
self._choices = {k: v for k, v in choices}
|
||||
super(ChoiceFieldSerializer, self).__init__(**kwargs)
|
||||
|
||||
def to_representation(self, obj):
|
||||
return self._choices[obj]
|
||||
return obj, self._choices[obj]
|
||||
|
||||
def to_internal_value(self, data):
|
||||
return getattr(self._choices, data)
|
||||
return self._choices.get(data)
|
||||
|
||||
|
||||
class WritableSerializerMixin(object):
|
||||
|
Reference in New Issue
Block a user