How to skip django DeleteModel operation
I have deleted a model, Django creates a DeleteModel
operation that I'd like to skip because i don't want to drop the table yet, but i also don't want to keep the model around. How can i skip dropping the table in this case but still delete the model?
I have tried commenting out the operation in the migration file but the next time you run makemigrations
command the operation is recreated.
When u create a model, Django create a table. If u don't need a model, just delete the table too, I couldn't imagine a scenario where u need a table but not the model for it. Even if u could do this, u couldn't manage the table anymore once u deleted the model (cause it's the "Django way" for CRUD operations on DB).
I think the best approach could be just don't use the model, or modify it when u will need a model for a future object of your project
I don't really see the problem having a model in models.py
you don't use, and thus acts as a placeholder.
But anyway, if you want to omit removing a model, you can plug in a custom database router preventing the actual migration:
# app_name/router.py
class PreventMigrationRouter:
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == 'my_app_label' and model_name == 'my_model_name':
return False
return None
with my_app_label
and model_name
the result of the model._meta.app_label
and model._meta.model_name
of the model you do not want to remove.
and then add your PreventMigrationRouter
as first router in the DATABASE_ROUTER
setting [Django-doc]:
# settings.py
DATABASE_ROUTERS = ['app_name.routers.PreventMigrationRouter']
Bearing in mind the potential risk of having a database state that doesn't match your models, you may wish to reconsider deleting the model and retaining the table.
However, if you wish to go ahead, you can use the --fake
flag when running a migration.
For example:
# Generate your migration file
python manage.py makemigrations
# Migration file generated with the name 0005_deletemodelname in the app test
# Fake running the migration
python manage.py migrate --fake test 0005_deletemodelname
Running a subsequent makemigrations
command will not generate a new DeleteModel
operation since Django considers the model to have already been dropped.