本文介绍了将数据导入Django的好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定期将数据导入Django项目。我需要告诉提供者我的数据是什么格式我想要收到的数据。我应该问Json,XML,CSV?

I would like to import data into a Django project regularly. I need to tell the provider of my data what formats I want to received the data in. Should I ask for it in Json, XML, CSV ?

Django通常如何处理这个问题?

How does one usually deal with this in Django?

推荐答案

Django有一个整体框架来导入名为Fixtures的数据。您可以在这里阅读可用的格式(JSON绝对有):

Django has a whole framework for importing data called Fixtures. You can read about the available formats (JSON is definitely there) here: https://docs.djangoproject.com/en/dev/howto/initial-data/

还有一些数据应该是什么样的例子。例如:

There are also examples of what the data should look like. Such as:

[
  {
    "model": "myapp.person",
    "pk": 1,
    "fields": {
      "first_name": "John",
      "last_name": "Lennon"
    }
  },
  {
    "model": "myapp.person",
    "pk": 2,
    "fields": {
      "first_name": "Paul",
      "last_name": "McCartney"
    }
  }
]

如果您保存为 beatles.json ,则可以通过运行 python manage.py loaddata / path /将其导入/ .json

If you saved that as beatles.json, you could import it by running python manage.py loaddata /path/to/beatles.json

这篇关于将数据导入Django的好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 03:11