django runpython

Solutions on MaxInterview for django runpython by the best coders in the world

showing results for - "django runpython"
Domenico
11 Feb 2016
1from django.db import migrations
2
3def forwards_func(apps, schema_editor):
4    # We get the model from the versioned app registry;
5    # if we directly import it, it'll be the wrong version
6    Country = apps.get_model("myapp", "Country")
7    db_alias = schema_editor.connection.alias
8    Country.objects.using(db_alias).bulk_create([
9        Country(name="USA", code="us"),
10        Country(name="France", code="fr"),
11    ])
12
13def reverse_func(apps, schema_editor):
14    # forwards_func() creates two Country instances,
15    # so reverse_func() should delete them.
16    Country = apps.get_model("myapp", "Country")
17    db_alias = schema_editor.connection.alias
18    Country.objects.using(db_alias).filter(name="USA", code="us").delete()
19    Country.objects.using(db_alias).filter(name="France", code="fr").delete()
20
21class Migration(migrations.Migration):
22
23    dependencies = []
24
25    operations = [
26        migrations.RunPython(forwards_func, reverse_func),
27    ]
28