Where should data import code live?

Hello, Django community! I’ve just been through the awesome “polls” tutorial series, and now I’m starting my first real project- a family tree.

I’m planning to write code to populate initial database records from an external GEDCOM (text) file- would that go in the model code? Is there a convention on where/how this sort of thing should live in a Django project?

Hi.

See the Fixtures docs. This targets testing really, but points to the serialization docs and the loaddata management command you can use for this.

You’d need to map your GEDCOM files to (say) JSON in the right format for import, but beyond that you should have everything you need.

I hope that helps.

1 Like

Ah perfect- I hadn’t read about Fixtures yet- thank you!!

1 Like

Additionally you could use a custom management command to import from your GEDCOM files. If this is data you always want loaded, you can consider a data migration or a post_migrate handler.

2 Likes

thanks Adam! Yes this is something any user will load at least once to start, and then would potentially re-load/replace multiple times per year

Hi Diane,

I personally don’t find Django’s Fixtures to be useful with data imports.

As Adam suggested, I would write a Django Management Command to read your data file. While I have never tried to access a GEDCOM data file (I see several on GitHub and pypi) and then import them one record at a time.

Django’s update_or_create() might be useful to you. I use it (or the alternative, get_or_create) when I write custom importers so that I can re-run them multiple times without duplicating what I have already imported.

1 Like