小编Ero*_*oyd的帖子

JSON数据转换为django模型

我需要将JSON数据转换为django模型.

这是我的JSON数据

{
  "data": [
    {
      "id": "20ad5d9c-b32e-4599-8866-a3aaa5ac77de",
      "name": "name_1"
    },
    {
      "id": "7b6d76cc-86cd-40f8-be90-af6ced7fec44",
      "name": "name_2"
    },
    {
      "id": "b8843b1a-9eb0-499f-ba64-25e436f04c4b",
      "name": "name_3"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

这是我的django方法

def get_titles():
    url = 'http://localhost:8080/titles/' 
    r = requests.get(url)
    titles = r.json()
    print(titles['data'])
Run Code Online (Sandbox Code Playgroud)

我需要的是转换为模型并传递给模板.请告诉我如何将JSON转换为Model.

python django json django-models python-2.7

5
推荐指数
1
解决办法
6713
查看次数

标签 统计

django ×1

django-models ×1

json ×1

python ×1

python-2.7 ×1