9

I need to apply a Django migration programmatically, for example this one:

class Migration(migrations.Migration):

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

    operations = [
        migrations.AlterIndexTogether(
            name='some_model',
            index_together=set([('some_field_one', 'some_field_two')]),
        ),
    ]

Is there a way to do it? I tried to use apply method, but couldn't figure out how to make it work.

Bob
  • 5,809
  • 5
  • 36
  • 53
  • 11
    you can try to use `from django.core import management management.call_command("migrate", "some_app", "0002", noinput=True)` for running migrate command from python. the migration 0002 in `some_app` must exist – devxplorer Oct 01 '16 at 20:40
  • @devxplorer I am using the projectstate and model state and trying to apply migrations. It is not getting applied. Django 3.0.3 and 3.1.x both don't work. DB mysql. Can you see if you are aware of this issue? I am probably missing some small thing . Even the create_model doesn't work https://stackoverflow.com/questions/64639768/unable-to-migrate-using-modelstate-and-projectstate-using-of-migrations-api-in-d – Gary Nov 04 '20 at 13:01

0 Answers0