I have some CSV data and I want to import into django models using the example CSV data:
1;"02-01-101101";"Worm Gear HRF 50";"Ratio 1 : 10";"input shaft, output shaft, direction A, color dark green";
2;"02-01-101102";"Worm Gear HRF 50";"Ratio 1 : 20";"input shaft, output shaft, direction A, color dark green";
3;"02-01-101103";"Worm Gear HRF 50";"Ratio 1 : 30";"input shaft, output shaft, direction A, color dark green";
4;"02-01-101104";"Worm Gear HRF 50";"Ratio 1 : 40";"input shaft, output shaft, direction A, color dark green";
5;"02-01-101105";"Worm Gear HRF 50";"Ratio 1 : 50";"input shaft, output shaft, direction A, color dark green";
I have some django models named Product. In Product there are some fields like name
, description
and price
. I want something like this:
product=Product()
product.name = "Worm Gear HRF 70(02-01-101116)"
product.description = "input shaft, output shaft, direction A, color dark green"
product.price = 100
You want to use the csv module that is part of the python language and you should use Django's get_or_create method
In my example the model teacher has three attributes first_name, last_name and middle_name.
Django documentation of get_or_create method
You can also use, django-adaptors
You declare a MyCsvModel which will match to a CSV file like this:
Anthony;27;1.75
To import the file or any iterable object, just do:
Without an explicit declaration, data and columns are matched in the same order:
If you're working with new versions of Django (>10) and don't want to spend time writing the model definition. you can use the ogrinspect tool.
This will create a code definition for the model .
The output will be the class (model) definition. In this case the model will be called Product. You need to copy this code into your models.py file.
Afterwards you need to migrate (in the shell) the new Product table with:
More information here: https://docs.djangoproject.com/en/1.11/ref/contrib/gis/tutorial/
Do note that the example has been done for ESRI Shapefiles but it works pretty good with standard CSV files as well.
For ingesting your data (in CSV format) you can use pandas.
Now, every row needs to be transformed into a dictionary and use this dict for instantiating your model (in this case, Product())
Done, check your database now.
You can give a try to django-import-export. It has nice admin integration, changes preview, can create, update, delete objects.
If you want to use a library, a quick google search for
csv
anddjango
reveals two libraries - django-csvimport and django-adaptors. Let's read what they have to say about themselves...The first requires you to write a model to match the csv file, while the second is more of a command-line importer, which is a huge difference in the way you work with them, and each is good for a different type of project.
So which one to use? That depends on which of those will be better suited for your project in the long run.
However, you can also avoid a library altogether, by writing your own django script to import your csv file, something along the lines of (warning, pseudo-code ahead):
It's super easy. Hell, you can do it interactively through the django shell if it's a one-time import. Just - figure out what you want to do with your project, how many files do you need to handle and then - if you decide to use a library, try figuring out which one better suits your needs.
You can use the django-csv-importer package. http://pypi.python.org/pypi/django-csv-importer/0.1.1
It works like a django model
And you just have to: CsvModel.import_from_file("my file")
That will automatically create your products.