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?
Django uses a database table called django_migrations . Django automatically creates this table in your database the first time you apply any migrations. For each migration that's applied or faked, a new row is inserted into the table. For example, here's what this table looks like in our bitcoin_tracker project: ID.
You have unapplied migrations; your app may not work properly until they are applied. Run 'python manage.py migrate' to apply them. How can I find out which migrations are unapplied without running migrate? One way to do this is to look at the django_migrations table in the DB and check which ones are applied.
You can revert by migrating to the previous migration. For example, if your last two migrations are: 0010_previous_migration. 0011_migration_to_revert.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With