Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good ways to import data into Django

Tags:

python

django

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 ?

How does one usually deal with this in Django?

like image 370
edu222 Avatar asked Oct 21 '25 01:10

edu222


2 Answers

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"
    }
  }
]

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

like image 150
Matt Williamson Avatar answered Oct 24 '25 20:10

Matt Williamson


If you want to support CSV or XML, I would advice to use django-adaptors

like image 29
trez Avatar answered Oct 24 '25 20:10

trez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!