0

TL;DR:

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

DETAILS:

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 settings.py, 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.

SercioSoydanov
  • 980
  • 10
  • 29
  • You have an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Why would you need to detect the migrations when the actual problem is that you have database queries running when they shouldn't be. Generally the cause of this is people making a query and looping over the results to create choices for a form field when they should really be simply using a queryset with a model choice field etc. Find the code causing the query and _fix_ it (If you don't know how to refactor the query without breaking anything ask _that_ question). Don't use a workaround. – Abdul Aziz Barkat Aug 30 '22 at 16:34
  • @AbdulAzizBarkat I think I've made myself very clear when I said `Model is so deeply integrated in the system so I cannot modify the depending models to use anything non dependent on this model.`. It's a rather large codebase and almost every model that resides on the system is depending on this config model, so fixing the actual problem is not an option until a rewrite. – SercioSoydanov Aug 30 '22 at 17:10
  • Django tests itself on management commands to find errors in code. Try `./manage.py migrate --skip-checks`. So in view when you import modelform class it initialize it with running query to db but there is no table because you want to migrate it:) – Waldemar Podsiadło Aug 30 '22 at 18:58

0 Answers0