Skip to main content

Import CSV files to django models

Project description

Django CSV Import

Ed Crewe - July 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.

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. Complete parsing test suite - more encoding tests, data types, deduplicating etc.

  2. Add other tests

  3. Add drop down of available models to admin

  4. Add option to create model from CSV file

  5. Add use of xlrd for Excel file upload

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

Changelog

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.2.tar.gz (11.0 kB view hashes)

Uploaded Source

Built Distribution

django_csvimport-0.2-py2.6.egg (27.0 kB view hashes)

Uploaded Source

Supported by

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