20

How can one query the django_migrations table from a view? For instance(What I have tried and of course not working)

from django.db import migrations

latest_migration = migrations.objects.all().order_by('-applied')[0]

If possible, how should one proceed?

Olfredos6
  • 808
  • 10
  • 19

1 Answers1

45

The migration model is defined in django.db.migrations.recorder. So you can should change your example code slightly:

from django.db.migrations.recorder import MigrationRecorder
latest_migration = MigrationRecorder.Migration.objects.order_by('-applied')[0]
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Works perfectly. Thank you very much! – Olfredos6 Apr 30 '18 at 15:09
  • 1
    Works for me on Django 3.2. I wish the use of this model was documented, but there's not a single reference (that I can find) to MigrationRecorder on docs.djangoproject.com. – odigity Mar 20 '22 at 20:38