loaddata for all fixtures in a project?

I have several apps, each app has several fixtures. Is there an option to easily load all fixtures at once?
From the help of manage.py loaddata and the docs, I know that you have to specify a name.
Is there a reason for that? Would be nice to have an --all flag or something similar.

You can use file globbing for all the files in a directory.
e.g. manage.py loaddata data/fixtures/*.yaml

Also see Fixtures | Django documentation | Django.

1 Like

I know, it matters, in which order loading the fixtures. How Django knows the proper order?

<conjecture>
The order doesn’t matter if database constraints are deferred and if all the data is loaded in one transaction. My assumption would be that that’s what loaddata is doing. I’ve never encountered an issue when doing bulk loads like this.
</conjecture>

I mean, if you got two tables and two json’s files (like polls.Questions.json and polls.Choise.json), and try to load polls.Choise.json first (on empty database), you got an error like

psycopg2.errors.ForeignKeyViolation: insert or update on table “polls_choise” violates foreign key constraint “polls_choise_group_id_5335682c_fk_questions”
DETAIL: Key (group_id)=(3) is not present in table “polls_choise”.

(ps I built error text by hand but meaning must be clear)

That is why I think about load order. May be I miss something, please advise me.

If you load them both in the same command, you shouldn’t get that error - unless you’re using a database that doesn’t support deferred constraints.

1 Like