Load Multiple Fixtures at Once

Why not create a Makefile that pulls in all your fixtures? eg something like:

load_all_fixtures: 
    ./manage.py loaddata path/to/fixtures/foo.json
    ./manage.py loaddata path/to/fixtures/bar.json
    ./manage.py loaddata path/to/fixtures/baz.json

And then at the shell prompt, run

make load_all_fixtures

(This kind of approach is also good for executing unit tests for certain apps only and ignoring others, if need be)


I have multiple apps on the project directory and have each app with its 'fixtures' directory. So using some bash I can do:

python3 manage.py loaddata */fixtures/*.json

And that expands all of the json files inside of the fixtures directory on each app in my project. You can test it by simply doing:

ls */fixtures/*.json


Using $ python manage.py loaddata myfixtures/*.json would work as Bash will substitute the wildcard to a list of matching filenames.


This thread shows up among the first results with a Google search "load data from all fixtures" and doesn't mention what IMO is the correct solution for this, ie the solution that allows you to load any fixtures you want without any wildcard tricks nor a single modification of the settings.py file (I also used to do it this way)

Just make your apps' fixtures directories flat (and not the usual Django scheme that e.g. goes app_name/templates/app_name/mytemplate.html), ie app_name/fixtures/myfixture.[json, yaml, xml]

Here's what the django doc says :

For example:

django-admin loaddata foo/bar/mydata.json

would search /fixtures/foo/bar/mydata.json for each installed application, /foo/bar/mydata.json for each directory in FIXTURE_DIRS, and the literal path foo/bar/mydata.json.

What that means is that if you have a fixtures/myfixture.json in all your app directories, you just have to run

./manage.py loaddata myfixture

to load all the fixtures that are located there within your project ... And that's it ! You can even restrict what apps you load fixtures from by using --app or --exclude arguments.

I'll mention that I use my fixtures only to populate my database while doing some development so I don't mind having a flat structure in my 'fixtures' directories ... But even if you use your fixtures for tests it seems like having a flat structure is the Django-esque way to go, and as that answer suggests, you would reference the fixture from a specific app by just writing something like :

class MyTestCase(TestCase):
    fixtures = ['app_name/fixtures/myfixture.json']