-- Enter the DB shell with `./manage.py dbshell`
-- Get latest ID
SELECT id FROM "django_admin_log" ORDER BY id DESC LIMIT 1;
-- Set the next value of the ID sequence to the result of the first query + 1
SELECT setval('django_admin_log_id_seq', LASTID + 1);
-- (replace `LASTID` with the result of the first query)
from django.db import connections
query = "SELECT setval('django_migrations_id_seq', (SELECT MAX(id) FROM django_migrations))"
cursor = connections['default'].cursor()
cursor.execute(query)
row = cursor.fetchone()