4

I've been playing with django import-export because it seems to be the obvious choice for anything import/export related in django and it's fantastic. Trouble is I can't find much in the way of docs on the import side of things and I'd like to be able to use it to import data through and cope with table relationships.

For example I've got a system for a cycling event, and an event has a table of various places managed by a user. I'd like that user to be able to import a file of details for people who will fill those places.

The places the user has have a FK to another table for cyclists in this event and the uploaded file would contain cyclists details. However the cyclist objects may not exist yet.

I've been working with django-import-export from an admin perspective, targeting specific model objects by ID from a CSV, but is it possible to cope with object relationships?

My thought was that from the view where a user can see their places in an event and add a cyclist to each one individually or upload a CSV file they could also download a CSV file. This would contain the ID for each place and a column for each piece of cyclist information I want to be supplied.

I know from the export side of this I was able to specify place__cyclist__name to get the name of a cyclist associated with a place. Is the same possible for importing data?

markwalker_
  • 12,078
  • 7
  • 62
  • 99
  • 1
    According to my experience, it's impossible, you have to override the before_import function. I've explain it there : http://stackoverflow.com/questions/28697797/dealing-with-import-of-foreignkeys-in-django-import-export/29071070#29071070 I don't know if there is a better way but this works... – HydrUra Mar 16 '15 at 07:11
  • 1
    I don't think it's impossible because there's a `ForeignKeyWidget` which *will allow you to use “natural keys” for both import and export.* – jnns Jan 28 '16 at 14:33

1 Answers1

2

It's answered here,

Adding foreignKey widget to django-import-export

And here

import m2m relation in django-import-export

For more details: django-import-export Widgets

Isanka Wijerathne
  • 3,746
  • 3
  • 26
  • 34