[Fixed]-Don't load 'initial_data.json' fixture when testing

6đź‘Ť

Quoting from Django Website:

If you create a fixture named
initial_data.[xml/yaml/json], that
fixture will be loaded every time you
run syncdb. This is extremely
convenient, but be careful: remember
that the data will be refreshed every
time you run syncdb. So don’t use
initial_data for data you’ll want to
edit.

So I guess there’s no way to say “okay, don’t load initial data just this once”. Perhaps you could write a short bash script that would rename the file. Otherwise you’d have to dig into the Django code.

More info here: http://docs.djangoproject.com/en/dev/howto/initial-data/#automatically-loading-initial-data-fixtures

👤kovshenin

2đź‘Ť

You might want to think about whether initial_data.json is something your app actually needs. It’s not hard to “manually” load your production data with ./manage.py loaddata production.json after running a syncdb (how often do you run syncdb in production, anyway?), and it would make loading your testing fixture much easier.

👤Jeff Hemphill

2đź‘Ť

If you want to have tables with no initial data, this code will help you:

edit tests.py:

from django.core import management

class FooTest(TestCase):

    @classmethod
    def setUpClass(cls):
        management.call_command('flush', interactive=False, load_initial_data=False)

this will remove your data and syncdb again without loading initial data.

👤andrenovax

Leave a comment