Django API响应以从序列化器中排除数据

时间:2019-04-24 10:07:27

标签: python django django-rest-framework

我有一个API端点,返回的数据如下:

{
    "customer_device_uuid": "a83e895d-d3b6-4816-b9f9-ee80feb22b36",
    "device_group": {
        "group_uuid": "ebd0990b-aeb5-46a4-9fad-82237a5a5118",
        "device_group_name": "Default",
        "color": "4286f4",
        "is_default": true
    },
    "status": [
        {
            "disk_space": 8,
            "battery_level": 8,
            "battery_health": "GOOD",
            "battery_cycles": 99
        }
    ]
}

我希望响应排除status。我尝试使用this question中建议的write_only字段,但这并未在响应中排除status

我的serializers.py

class DeviceStatusSerializer(serializers.ModelSerializer):
    class Meta:
        model = DeviceStatus
        fields = ('disk_space', 'battery_level', 'battery_health', 'battery_cycles')


class CheckinSerializer(serializers.ModelSerializer):
    device_group = DeviceGroupSerializer(many=False, read_only=True, source='group_uuid')
    status = DeviceStatusSerializer(source='customer_device', many=True, read_only=True)

    class Meta:
        model = CustomerDevice
        fields = ('customer_device_uuid', 'customer_uuid', 'device_id_android', 'device_group', 'status')
        extra_kwargs = {
            'customer_uuid': {'write_only': True},
            'device_id_android': {'write_only': True},
            'status': {'write_only': True}
        }

如何从响应中排除status

0 个答案:

没有答案