3

My fixtures are loading in an incorrect order, so they fail to install - app A creates a model with a foreign key to a model in app B, but Django tries to load the initial data for app B first.

How can I control the order in which the initial data is installed?

Dor
  • 902
  • 4
  • 24

1 Answers1

1

You can specify the dirs for fixtures in settings.py FIXTURE_DIRS, but these are appended after the fixtures app directories. Thus you can rename app_B/fixtures to app_B/slow_fixtures, and add app_B/slow_fixtures to FIXTURE_DIRS.

https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FIXTURE_DIRS

xbello
  • 7,223
  • 3
  • 28
  • 41
  • Thanks, this will work. However, I feel it's sort of a hack... Is there a more elegant solution? – Dor May 23 '14 at 17:46
  • https://github.com/django/django/blob/master/django/core/management/commands/loaddata.py, method fixture_dirs. __Maybe__ Django loads the fixtures in the same order that `INSTALLED_APPS`, but I don't have time to dive in the code. Try to put app_A before app_B there. – xbello May 23 '14 at 20:17
  • 2
    @Dor, in https://github.com/django/django/blob/master/django/apps/registry.py you can see in `def populate()` that they use an OrderedDict to store the Apps in the same order that they are in `INSTALLED_APPS`. In `def fixture_dirs()` the same Dict is used, so I assume the loading order of fixtures is the same as `INSTALLED_APPS`. – xbello May 26 '14 at 16:11