有没有办法让django API响应为
{"1":"country1", "2":"country2"}
而不是
[{"id":1,"country":"country1"},{"id":2,"country":"country2"}]
答案 0 :(得分:1)
您可能需要简单地转换数据:
src = [{"id":1,"country":"country1"},{"id":2,"country":"country2"}]
result = dict([(data.get('id'), data.get('country')) for data in src ])