Django Rest Framework custom field - just a couple of values ​​(no field name as dict key)

Hi I only want to provide a value pair without keys in a REST service:

take a look at my serializers.py :

 class TranslationSerializer(serializers.ModelSerializer):
        translated_term = serializers.CharField(read_only=True)

        class Meta:
                model = Translation
                fields = ('language','translated_term')

class VocabSerializer(serializers.ModelSerializer):
       ...
        translates = TranslationSerializer(many=True, read_only=True)
       ...

        class Meta:
        model = Vocab
        fields = ( ..., 'translates',...)

      

The result is the following:

"translates": [
        {
            "language": "EN",
            "translated_term": "Chair"
        }
        {
            "language": "IT",
            "translated_term": "asd"
        }
    ],

      

as you can see this result shows the field name as a dict key next to the value of that field. but I would like to have a different structure. As shown below:

    "translates": [
        {
            "EN": "Chair", "IT":"asd"
        }
    ],

      

This example shows a key / value pair, where key is the value of the language field and the dict value is the value of the translated field.

How can I only show a couple of 'language' field values ​​with a 'transl' field without the field name?

+1


source to share


2 answers


You can implement it by customizing the list serializer :



class TranslationListSerializer(serializers.ListSerializer):
    def to_representation(self, data):
        r = super().to_representation(data)

        return { item['language']: item['translated_term'] for item in r }

class TranslationSerializer(serializers.ModelSerializer):
    translated_term = serializers.CharField(read_only=True)

    class Meta:
            model = Translation
            fields = ('language','translated_term')
            list_serializer_class = TranslationListSerializer

      

+6


source


By using create to_representation for the serializer you can control your representation in the dictionary.

 class TranslationSerializer(serializers.ModelSerializer):
        translated_term = serializers.CharField(read_only=True)

        def to_representation(self, instance):
            # get the instance -> Dict of primitive data types
            representation = super(TranslationSerializer, self).to_representation(instance)

            # manipulate returned dictionary as desired
            language = representation.pop('language')
            translated_term = representation.pop('translated_term')

            # repackage
            repackaged = {language: translated_term}

            return repackaged

        class Meta:
                model = Translation
                fields = ('language','translated_term')

      

This will generate JSON in the format



"translates": [
        {
            "EN": "Chair"
        },
            "IT": "asd"
        }
    ],

      

However, this is not exactly how you want it to look, perhaps a custom "to_representation" for the VocabSerializer can accomplish this.

+1


source







All Articles