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?

2
  • 1
    According to my experience, it's impossible, you have to override the before_import function. I've explain it there : stackoverflow.com/questions/28697797/… I don't know if there is a better way but this works... Commented Mar 16, 2015 at 7: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. Commented Jan 28, 2016 at 14:33

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.