Django 3.2.7 - Detect Currently Running Command From Code


Is it possible from within a django project to detect which command is currently running?


We have a Django project (running 3.2.7) running which migrations are a little mixed up.

There is a config model which has been developed way after the initial migrations has been done. This config model is basically a Django ORM model mapped to a DB object and has some caching features (which I will not go into details).

The actual code running on servers never experience any problems, since config model is already provisioned on DB. But right now I'm trying to dockerize the whole project and I need to apply migrations from scratch. When I try to apply the migrations to a fresh db,

  • It reads, which contains ROOT_URLCONF variable, that is being directed to another .py file
  • this .py file contains urlpatterns list, which contains references to various django models
  • Some of these models includes reference to the config model that I've mentioned earlier. When they are imported, those models are trying to pull configs from this config model, which is not initialized yet on the DB, causing psycopg2.errors.UndefinedTable: relation "core_config" does not exist error.

Model is so deeply integrated in the system so I cannot modify the depending models to use anything non dependent on this model.

My main idea is to detect if makemigrations command is running and returning an empty urlpatterns object if this is the case.

Not sure if this is the best approach, but this is the best I've been able to come up with. If you have any other solution, I'm all ears.

Back to Top