суббота, 6 сентября 2014 г.

Loading fixtures with initial data using new Django's core migrations

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

  1. Be sure your database is up to date with your code.
  2. Make empty migration
    python manage.py makemigrations --empty my_app
  3. 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)
  4. Apply migration
    python manage.py migrate
  5. Run tests and be sure all good.

Комментариев нет: