Django Rest Framework custom field only pair of values (without fieldname as dict key)

ⅰ亾dé卋堺 提交于 2019-12-13 01:47:34

问题


Hi i want to provide only pair of values without keys on REST service:

take a look on 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 name of the field as dict key next to the value of that field. but i would like to have a different structure. Like the following:

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

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

How can i show only the pair of values of 'language' field with 'translated' field without the name of field?


回答1:


You may 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



回答2:


By using creating a custom to_representation function for the serializer, you can have control over your dictionary representation.

 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 of

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

However, this is not exactly how you desire it to look, perhaps a custom 'to_representation' for the VocabSerializer can preform that.



来源:https://stackoverflow.com/questions/31583445/django-rest-framework-custom-field-only-pair-of-values-without-fieldname-as-dic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!