How do I return the human readable element of a Choice field in a Serializer Class. Sample code below.
from rest_framework import serializers
from model_utils import Choices
from django.utils.translation import ugettext_lazy as _
COMPANY_TYPE = Choices(
(1, 'Public', _('Public Company')),
(2, 'Private', _('Private Company')),
(3, 'Other', _('Other Type')),
)
class CompanySerializer(serializers.ModelSerializer):
company_type = serializers.ChoiceField(choices=COMPANY_TYPE)
company_type_name = serializers.ReadOnlyField(source=COMPANY_TYPE[1]) # <=== This is the issue
class Meta:
model = Company
fields = ('id', 'title', 'company_type', 'company_type_name')
If say an entry in the company table has company_type = 1
, and a user makes an API request, I want to include the extra field of company_type_name
with the value Public Company
.
So the issue is am unable to pass the current value of company_type
to the serializer so that it can return the String value of the Choice Field.