如何重构json对象django休息框架?

时间:2017-06-15 07:39:19

标签: json django python-3.x django-rest-framework

这是json

Django回复
{
    id:"123"
    latitude: "37.5111",
    longitude: "126.9743"
}

想要输出,就像这样:

"123": {
     latitude: "37.5111",
     longitude: "126.9743"
}

serializers.py

class SearchSerializer(ModelSerializer):

    class Meta:
        model = IpGeo
        fields = [
            # 'id',
            # 'metadata',
            'latitude',
            'longitude'
        ]

views.py

class Search(ListAPIView):

    queryset = IpGeo.objects.all()
    serializer_class = SearchSerializer

1 个答案:

答案 0 :(得分:1)

serializers.py

来自rest_framework导入序列化程序

  with open(path_to_save, "wb", encoding="utf-8") as save_file:
        save_file.write(data)