Serialise choice text for IntegerField with choices
You can get a read-only serializer field with the verbose value of a model field with choices through the get_FOO_display
method. This method is added automatically when setting choices on a field. You can set that method as the source for a character field.
For endpoints that also support writing data, I would recommend to add the "normal" field, and another read-only field with the _name
extension.
In your example, the following should produce the output you are looking for. The accessibility_name
is read-only, while the accessibility
is there to allow writing / updating values.
class HouseSerializer(serializers.ModelSerializer):
accessibility_name = serializers.CharField(source='get_accessibility_display')
class Meta:
model = House
fields = ('accessibility', 'accessibility_name', )
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
.