How to get a name of last migration programmatically?

This works on Django 1.11/1.8/2.1 & 3.0.4:

from django.db.migrations.recorder import MigrationRecorder

last_migration = MigrationRecorder.Migration.objects.latest('id')
print(last_migration.app)     # The app where the migration belongs
print(last_migration.name)    # The name of the migration

There doesn't seem to be documentation for this command, but here you may find the source code which is documented properly.


To store information about applied migrations Django uses plain table and it is accessible as @classproperty through the MigrationRecorder class:

from django.db.migrations.recorder import MigrationRecorder

lm = MigrationRecorder.Migration.objects.filter(app='core').last()

It is also easy to retrieve this information from the command line:

Get the last applied migration for the particular app

python manage.py showmigrations --list <app_name> | grep "\[X\]" | tail -1

Get the ordered list of unapplied migrations

python manage.py showmigrations --plan | grep "\[ \]"