My django application needs data to work properly, so in certain migration I loaded data using the recommended method by almost all stack overflow answers:
from django.core.management import call_command
def load_fixture(apps, schema_editor):
call_command('loaddata', 'fixture_name', app_label='my_app')
class Migration(migrations.Migration):
...
operations = [
migrations.RunPython(load_fixture),
]
In the following migration, I deleted a field and added another field. So far so good.
Now I want to test my application, but when the test database is being created, the 'call_command' above fails because the fixtures don't specify values for the field created in the next migration. I suspect this is because call_command
isn't loading the objects on the test database but on settings.DATABASES['default']
.
How should I load fixtures in a migration so that I can build test databases?