Posts Django migrations
Post
Cancel

Django migrations

How create a named empty migration in a specific django app?

./manage.py makemigrations --empty my_django_app_name -n name_of_my_migration

How run some python code in my migration?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from django.db import migrations

def my_function_with_my_python_code(apps, schema_editor):
    print('write your python code here')


class Migration(migrations.Migration):

    dependencies = [
        ('django_app_name', 'name_of_migration_dependencies'),
    ]

    operations = [
        migrations.RunPython(my_function_with_my_python_code),
    ]

How to call some Django model in my migration?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from django.db import migrations

def my_function_with_my_python_code(apps, schema_editor):
    MyModel = apps.get_model('django_app_name', 'MyModel')
    res = MyModel.objects.all()
    print('write your python code here')


class Migration(migrations.Migration):

    dependencies = [
        ('django_app_name', 'name_of_migration_dependencies'),
    ]

    operations = [
        migrations.RunPython(my_function_with_my_python_code),
    ]
This post is licensed under CC BY 4.0 by the author.