How to setup django migrations to be always the last one applied

django, django-migrations, python

Solution

You can subclass `migrate` command and put this code after `super` call

# project/myapp/management/commands/custom_migrate.py
from django.core.management.commands.migrate import Command as MigrateCommand

class Command(MigrateCommand):
    def handle(self, *args, **options):
        super().handle(*args, **options)
        # put your code from 002_auto.py here

This command should be added to app that is in your INSTALLED_APPS. And then you can call it like this

python manage.py custom_migrate

Read more about custom commands https://docs.djangoproject.com/en/1.10/howto/custom-management-commands/

Problem

I have django application and migration file `002_auto.py`, which uses Django RunPython to alter database. I have no idea how much migrations files would be created in future, but I want the file `002_auto.py` to be applied as the last part of migration process. How to set that migrations to be executed as the last one while performing django migrations without need to perform any manual steps each time I want to execute `migrate` command (or altering `dependencies` variable each time i've added new migrations)? p.s. I've looked into django migrations documentation and other articles without success.

Original source