Skip to main content

Import CSV files to django models

Project description

Django CSV Import

Ed Crewe - October 2011

Overview

django-csvimport is a generic importer tool to allow the upload of CSV files for populating data. The egg installs an admin cvsimport model that has a file upload field. Add a new csvimport and upload a comma separated values file or MS Excel file.

The upload triggers the import mechanism which matches the header line of the files field names to the fields in the selected model. Importing any rows that include all required fields. Optionally required fields can be specified as part of the upload. By default duplicate value rows are not inserted.

The import can also be run as a custom command, ie manage.py csvimport filename for possible use via cronjob etc.

The core import code was based on http://djangosnippets.org/snippets/633/ by Jonathan Holst. It adds character encoding handling, model field and column autodetection, admin interface, custom command etc.

NB: There is another similar application django-batchimport but it not packaged, requires Excel files and doesnt provide a custom command for batch usage.

Custom command

Use manage.py csvimport –mappings=’’ –model=’app_label.model_name’ importfile.csv

For mappings enter a list of fields in order only if you dont have a header row with matching field names - or you want to override it, eg.

–mappings = ‘column1=shared_code,column2=org(Organisation|name)’

where (model|foreign key field) is used to specify relations if again, you want to override what would be looked up from your models.

Admin interface import

Just add a csvimport item, fill in the form and submit. Failed import rows are added to the log field.

Demonstration installation instructions

To see how it works, you can install a demo easily enough eg. via virtual environment, then use the tests settings to have some sample models for importing data, and the fixtures are sample csv files.

  • virtualenv install mysite

  • cd mysite

  • bin/pip install django

  • bin/pip install django-csvimport

Add the following django-admin.py to the bin directory:

>>> #!/path/to/mysite/bin/python
>>> from django.core import management
>>> import os
>>> os.environ["DJANGO_SETTINGS_MODULE"] = "csvimport.tests.settings"
>>> if __name__ == "__main__":
>>>     management.execute_from_command_line()
  • Run

  • bin/django-admin.py syncdb

  • bin/django-admin.py runserver

  • Go to http://127.0.0.1:8000/admin in your browser

  • Click on add CSVImport

  • Pick the django-csvimport/csvimport/tests/fixtures/countries.csv and upload it

  • Check to see if the Country model is now populated.

Acknowledgements

This egg was created as part of a django dash at the House of Omni, Bristol UK, organised by Dan Fairs and the local django users group, DBUG. It was a core component for an application for aid agency supply chain sharing, prompted by Fraser Stephens of the HELIOS foundation and developed by Ed Crewe and Tom Dunham.

TODO

  1. Add more tests

  2. Add option to create model from CSV file

  3. Add use of xlrd for Excel file upload

  4. Improve related model creation - use of related csv file?

Changelog

0.4 - Add settings to allow demo site install from tests - 4th October 2011

  1. Add demo countries.csv file

  2. Add MEDIA_ROOT for file uploads

0.3 - Fix issue with adding in admin - 25th Sept 2011

  1. Fix empty mapping submitted

  2. Remove non-generic filename country processing code

  3. Use get_models to populate drop down for admin import form

  4. Add upload message about whether mapping is from CSV header row

  5. Ensure header row only used if mapping is not supplied.

0.2 - Initial beta release version - 29th July 2011

  1. Build it as a django-csvimport egg

  2. Create csvimport management command

  3. Improve character set detection and unicode handling

  4. Add admin csvimport model to upload CSV files with logging of import

  5. Use file upload save to trigger csvimport command

  6. Autodetect column mappings by matching model fields and CSV header text

  7. Add a deduplicate feature

  8. Start to add test suite with data / app for HELIOS cross agency supply chain data sharing http://www.helios-foundation.org/cbha-project/index_html

Ed Crewe

0.1 - Unreleased

  1. Use django snippet as starting point http://djangosnippets.org/snippets/633/

Jonathan Holst

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-csvimport-0.4.tar.gz (19.8 kB view details)

Uploaded Source

Built Distributions

django_csvimport-0.4-py2.7.egg (38.0 kB view details)

Uploaded Egg

django_csvimport-0.4-py2.6.egg (37.6 kB view details)

Uploaded Egg

File details

Details for the file django-csvimport-0.4.tar.gz.

File metadata

File hashes

Hashes for django-csvimport-0.4.tar.gz
Algorithm Hash digest
SHA256 d28d43414ff5bd99c72620f345235ab70d28e1a23bad2af4182af969427c4e5c
MD5 3d1417273cade8d2cf97fce3ff7362cb
BLAKE2b-256 e635d559a9ad4705646e7b2abaad009e38edfabe9c06070cd63f7ccec3eaa6ec

See more details on using hashes here.

File details

Details for the file django_csvimport-0.4-py2.7.egg.

File metadata

File hashes

Hashes for django_csvimport-0.4-py2.7.egg
Algorithm Hash digest
SHA256 468a205659f0f5607da5342063e5cef2ddcf97883353a969f9efaa4cfd69ab52
MD5 1c2c50a84085d1e80a674cd6852f29ae
BLAKE2b-256 8edb0893c090b21521dcd505bfee786104d990c1370a4c7e548e213c9615fddf

See more details on using hashes here.

File details

Details for the file django_csvimport-0.4-py2.6.egg.

File metadata

File hashes

Hashes for django_csvimport-0.4-py2.6.egg
Algorithm Hash digest
SHA256 ff2d570006c9b1790ed040da4e6996a9c86a658730644980e26d49f63bb81148
MD5 4f342f5834860c49571eb0b1fc87f75e
BLAKE2b-256 1f734e35c9ae84d09cb2f0e2b944c8e158eaf4f92022ba3fcd150c58fb136f84

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page