When writing backend in Django, your project may overwhelm with the migrations in the apps and you would want to reduce that number down to only into a few migrations. That's what this article is about.
There is a simple command mentioned in Official Django documentation.
python manage.py squashmigrations myapp <to_migration_number>
Above command will sqaush migrations inside 'myapp' app from initial to the migration number you mention in the argument above.
However in many use cases you would need to squash the migrations from a specific migration number to another(e.g. 0003 to 0009). The command to achieve that is:
python manage.py squashmigrations <appname> <squashfrom> <squashto>
It's simple you mention 'from' and 'to' migration numbers.
It creates a migrations. The only difference you will see this migrations file form other is that it has:
Migration class's first statement is an array of tuples for each migrations squahed. E.g. replace = [('myapp', '0003_auto...')].
I had a situation when I didn't want the squashed migrations to be deployed in the production environment. I tried to explore but couldn't find a better solution. If you have one please post in the comment. I also have asked a question on stackoverflow but no answers.
Note: This is a dirty way to revert the migrations. Avoid editing migrations unless you really need to.
Solution:- Remove the statement with replace array. Usually the first statement in the squashed migrations file's Migration class.
1. Check the help section for details arguments. Based on your need you would find different arguments you would need to pass to squash the migrations.
python manage.py help squashmigrations
2. Give a name to squashed migrations
python manage.py squashmigrations app_label [start_migration_name] migration_name
Learn to write code in python. A complete course on python programming!