1
0
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:
Jeremy Stretch
2017-02-16 14:37:21 -05:00
parent b71566f206
commit 21281789e0
2 changed files with 5 additions and 5 deletions
netbox
dcim/api
utilities

@ -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):