Loading fixture into db using new django core migrations
I like new django's migrations mechanism. But there is some steps we need to do to meet new standarts.
initial_data was marked as deprecated. But I want to load some data migration with user groups automatically. There is a way that helps me to do that. At first you should have, at least, initial migration. To load data into database you can follow this prompts
- Be sure your database is up to date with your code.
- Make empty migration
python manage.py makemigrations --empty my_app
- Load new migration file and make it like this:
from __future__ import unicode_literals from django.core.management import call_command from django.db import models, migrations from my_app import settings class Migration(migrations.Migration): def load_migration(apps, schema_editor): path_to_fixture = os.path.join( settings.BASE_DIR, 'formbuilder_core/fixtures/groups.json') call_command('loaddata', path_to_fixture) dependencies = [ ('formbuilder_core', '0001_initial'), ] operations = [ migrations.RunPython(load_migration) - Apply migration
python manage.py migrate
- Run tests and be sure all good.
Комментариев нет:
Отправить комментарий