3

I want to have initial data for tables like Users and Options.

For old django the fixtures was very easy way to do but now django says to do it in migration way which i don't fully understood.

Now i already have 10 migrations in my migrations folder. I am confused where do i keep my initial data migration file .

If i make it like 0011_initial_data and put it in other migration then it will get lost in long list of migration and its not easy noticeable to new user to see what is that . and also if someone squash the migration then no one will know if there is some data in there.

I want to keep it separate in some folder called data migration . How can i do that

This is the example code from their site. But where do i place it so that it don't get mixed up

# -*- coding: utf-8 -*-
from django.db import models, migrations

def combine_names(apps, schema_editor):
    # We can't import the Person model directly as it may be a newer
    # version than this migration expects. We use the historical version.
    Person = apps.get_model("yourappname", "Person")
    for person in Person.objects.all():
        person.name = "%s %s" % (person.first_name, person.last_name)
        person.save()

class Migration(migrations.Migration):

    dependencies = [
        ('yourappname', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(combine_names),
    ]
user3214546
  • 6,523
  • 13
  • 51
  • 98
  • Here is the answer: http://stackoverflow.com/a/25981899/548165 – catavaran May 11 '15 at 00:46
  • @catavaran i want to keep that migration separate from other migration . IN that questions they Just keep fixture in separate folder but migration still is with other files – user3214546 May 11 '15 at 01:12
  • @user3214546 That's not possible, Django does not support multiple migration folders for a single app. – knbk May 11 '15 at 01:33

1 Answers1

5

Like @knbk said, you can't take the migration out of it's location. However, if you want your migration in between your other migrations, but have the fixture data in a separate file, you can do this:

from django.core.management import call_command
from django.db import models, migrations


class Migration(migrations.Migration):

    def load_data(apps, schema_editor):
        call_command("loaddata", "initial_data.json")

    dependencies = [
        ('other_app', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(load_data),
    ]

Django will look for the fixture file the same way it always has and your data gets loaded when you migrate your db.

Jorick Spitzen
  • 1,559
  • 1
  • 13
  • 25
  • suppose after running this migration , i change the data in my fixture file , then what will happen . how will same migration run again – user3214546 May 12 '15 at 01:40
  • You can roll back to the migration before your data migration, then simply call migrate and the migration will be re-applied, along with all later migrations. https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-migrate – Jorick Spitzen May 12 '15 at 07:26