Django将JSON导入/重新格式化为模型 [英] Django import/reformat JSON into Model

查看:181
本文介绍了Django将JSON导入/重新格式化为模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定期将json数据导入Django.将以下数据导入Django模型的最佳方法是什么.

I like to periodically import json data into a Django. What's the best way to import the data below to a Django model.

据我所知,使用 Django夹具.由于我的模型看起来像这样,并且我无法更改给定的JSON,因此导入数据或重组JSON以将其用作固定装置的最佳方法是什么.

As far as I've seen is the best approach to use Django fixtures. Since my Model looks like this and I can't change the given JSON, what's the best way to import the data or restructure the JSON to use it as a fixture.

Django模型

class Item(models.Model):
    item_name = models.CharField(max_length=250)
    timestamp = models.DateTimeField()
    value01 = models.DecimalField(max_digits=7, decimal_places=2)
    value02 = models.DecimalField(max_digits=7, decimal_places=2)


我要存储的数据:

Item1  |  2017-04-18 09:24:46  |  15.00 | 12.68
Item1  |  2017-04-18 09:25:44  |  14.92 | 12.42


提供JSON:

[
  {
    "id": 1,
    "timestamp": "2017-04-18 09:24:46",
    "details": {
      "id": 843,
      "color": "white"
    },
    "item": {
      "id": 1691,
      "category": "1",
      "item_type": {
        "id": 14,
        "name": "Item1"
      }
    },
    "itemdatavalues": [
      {
        "id": 227,
        "value": "15.00"
      },
      {
        "id": 228,
        "value": "12.68"
      }
    ]
  },
  {
    "id": 2,
    "timestamp": "2017-04-18 09:25:44",
    "details": {
      "id": 843,
      "color": "white"
    },
    "item": {
      "id": 161,
      "category": "1",
      "item_type": {
        "id": 14,
        "name": "Item1"
      }
    },
    "itemdatavalues": [
      {
        "id": 283,
        "value": "14.92"
      },
      {
        "id": 284,
        "value": "12.42"
      }
    ]
  }
]

推荐答案

我编写了一个简单的脚本来解析输入文件并生成固定装置:

I wrote a simple script to parse the input file and generate a fixture:

import json
import copy

with open('/tmp/data.json') as dataf, open('/tmp/output.json', 'w') as out:
    data = json.load(dataf)
    newdata = []
    for i, block in enumerate(data):
        new = dict(model="appname.Item", pk=block['id'])
        new['fields'] = dict(item_name=block['item']['item_type']['name'],
                             timestamp=block['timestamp'],
                             value01=block['itemdatavalues'][0]['value'],
                             value02=block['itemdatavalues'][1]['value'])
        newdata.append(copy.deepcopy(new))
    json.dump(newdata, out, indent=2)

使用您的输入,结果是:

Using your input, this results in:

[
  {
    "pk": 1,
    "model": "appname.Item",
    "fields": {
      "item_name": "Item1",
      "timestamp": "2017-04-18 09:24:46",
      "value01": "15.00",
      "value02": "12.68"
    }
  },
  {
    "pk": 2,
    "model": "appname.Item",
    "fields": {
      "item_name": "Item1",
      "timestamp": "2017-04-18 09:25:44",
      "value01": "14.92",
      "value02": "12.42"
    }
  }
]

适合用作固定装置.显然,您必须修复appname.

Which is suitable for use as a fixture. Obviously, you will have to fix the appname.

这篇关于Django将JSON导入/重新格式化为模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆