Serialise choice text for IntegerField with choices
django-rest-framework
Solution
Set the choices in the serializer field with the raw values like so...
ACCESSIBILITY_CHOICES = (
('Full', 'Full'),
('Partial', 'Partial'),
('None', 'None')
)
Then take a look at overriding the 'to_native' method so that the string values get translated into their integer equivalents.
That should give you an external API that uses the string representations, but a backend that uses the integer representations.
Update 2019: DRF now uses different method name to convert from/to internal, see docs. to name them: `to_representation` and `to_internal_value`, instead of `to/from_native`.
Problem
I'd like to serialise a model with a lot of choices like such: ``` class House(models.Model): ACCESSIBILITY_CHOICES = ( (ACCESSIBILITY_FULL, 'Full'), (ACCESSIBILITY_PARTIAL, 'Partial'), (ACCESSIBILITY_NONE, 'None') ) accessibility = models.IntegerField(max_length=1, choices=ACCESSIBILITY_CHOICES, null=True) ``` I love that the default serializer such as: ``` class HouseView(generics.ListCreateAPIView): model = House serializer_class = HouseSerializer class HouseSerializer(serializers.ModelSerializer): class Meta: model = House ``` works great if I want just the integer value ``` {accessibility:1} ``` However, what I'd like to get ``` {accessibility:'Full'} ``` Help is kindly appreciated. Many thanks.