marcus-s
3/11/2016 - 11:47 PM

How to create a function inside a Django migration file

How to create a function inside a Django migration file

# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2016-03-11 23:37
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    def foo(apps, schema_editor):
        FooBar = apps.get_model("bar", "FooBar")
	      fb = FooBar(name="foo and bar")

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='FooBar',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=10)),
            ],
        ),
	      migrations.RunPython(foo),
    ]