Serialise choice text for IntegerField with choice

2020-08-26 04:01发布

问题:

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.

回答1:

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.



回答2:

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', )