Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load Django fixtures from all apps?

I'm using fixtures in my Django application but only two apps are getting their fixtures loaded.

When I manually run loaddata with --verbosity=2 I can see that it only looks in two apps although I have more with fixtures directories created inside.

All apps are correctly installed in settings.py.

From the documentation it seems that Django is supposed to search in the fixtures/ directory of every installed application.

Any ideas why some apps are ignored ?

like image 216
Franck Avatar asked Mar 02 '11 12:03

Franck


2 Answers

Initial_data gets imported each time you do syncdb. As fair as I remember, it also overwrites any changes you have done manually.

To load other fixtures, you have to use manage.py loaddata fixturename. That works well if you have you use a common naming scheme in all your apps. If you don't, you have to give loaddata the name of each one, or use find to get the list of fixtures and exec loaddata in each one of them:

EDIT: (as I add manage.py to /bin in the virtualenv when I install the django package I only use manage.py, if you don't you will need python manage.py loaddata of course)

find . -name "*.json" -exec manage.py loaddata {} \;

I use this in a fabfile to automate staging installations:

def load_all_fixtures():
    """Loads all the fixtures in every dir"""
    with cd(env.directory):
        run("""
            source /usr/local/bin/virtualenvwrapper.sh &&
            workon %s &&
            find -L . -name "*.json" -exec manage.py loaddata {} \;

            """ % env.virtualenv )
like image 115
ashwoods Avatar answered Oct 11 '22 04:10

ashwoods


Try calling this way

python manage.py loaddata initial_data

OR programmetically you can call it like

from django.core.management import call_command
call_command('loaddata', 'initial_data', verbosity=3, database='default')
like image 22
shahjapan Avatar answered Oct 11 '22 04:10

shahjapan