Cancel django settings cache without restarting the server?

I am working with a django application that includes multiple databases - dynamically adding multiple databases. Every time a new customer subscribes, we give them a new database. The settings file can load them dynamically (for example, I run the script shell on the server and which updates the database definition without changing any code - if you are really interested how to tell me), but since the caches of uwsgi / django settings, I need to run uwsgi only to recognize a new database.

Any tips on forcing django to reload the settings or invalidate the settings cache? It can be from the command line, etc. At the moment I am using import settings and not from django.conf import settings , because it worked in the past, but I can easily go back.

Thanks!

ps - if the answer is: "you are dummy, do not use dynamic multiple databases", well, also good);

+4
source share
1 answer

Primarily. If you can get away from multiple DO databases, Django only supports this well if the list of databases is fixed. If you can afford a separate database per client, you can probably afford separate wsgi processes (and separate DATABASES settings) for different clients. Then you simply create a new wsgi process for the new client and do not have to change the settings.

If you still want to do what you are planning, here are some tips:

Reloading settings is not an option dynamically. There are a lot of problems. Even if you fight with everyone, they can return at a time when you are not expecting, and you will spend hours (if not days) on debugging.

If your database settings are consistent (that is, the same password and user for all clients), you might consider using defaultdict for the DATABASES variable. The default value of dict has a factory parameter. You can have something like this in the factory:

 def database_configuration_factory(name): # check if database exists (ie. raw SQL) return { 'NAME': name, ... } 
+1
source

Source: https://habr.com/ru/post/1415213/


All Articles