Django自定义序列化器响应

时间:2017-10-09 10:43:11

标签: django django-rest-framework

有没有办法让django API响应为

{"1":"country1", "2":"country2"} 

而不是

[{"id":1,"country":"country1"},{"id":2,"country":"country2"}]

1 个答案:

答案 0 :(得分:1)

您可能需要简单地转换数据:

src = [{"id":1,"country":"country1"},{"id":2,"country":"country2"}]
result = dict([(data.get('id'), data.get('country')) for data in src ])