Django how to access the request object in settings.py

How can I access the request object inside settings.py? Perhaps by creating a temporary settings object, changing it, and then telling the rest of the chain to use this instead of the usual settings.py settings?

I need to decide which DB connection to use.

As an additional question. If I had something like 5,000 database connections, would settings.py be as efficient as storing them in sqlite db on the web interface? And would it be just as painless to update connections? Or do you need to restart the server to catch the changes in settings.py?

Edit: To find out why I might need a lot of connections. I am creating a webapp. This is SaaS and, like many others, each account will have a subdomain so that they can create users and they will not need to interact with any other subdomain / account. Then it would be nice to limit each account to a separate database. This provides added security and simplifies the application. There are many more advantages, but this should illustrate this well. That's why I could get many different databases (but not many different physical servers, if that matters).

+4
source share
4 answers

Just add this for anyone looking for the same. This is currently not possible. I created a function request for a Djugo error (# 13056, I think) and presented a prototype fix, but I don’t think it will be included in the near future, and there are probably a lot of errors in it.

I moved the project to Flask since it has a g object that is perfect for this.

+1
source

If I understand this right, you can use the new django db routing and select the database on the fly based on the model instance (for example, your user) without having to use using() .

+1
source

Django ORM is not intended to switch average database credentials. Perhaps you would be happy with something more DIY, like SQLAlchemy .

0
source

I reviewed this issue on a site I used recently and decided that Apache / mod_wsgi would do the job. This solution adds some memory and CPU overhead, but for my application this is the best way to maintain flexibility.

Apache.conf:

 SetEnv DJANGO_TEMPLATE_DIR '/usr/local/www/apache22/data/django/templates/' <VirtualHost *:80> ServerName encendio.whatever.com ServerAdmin your_admin@whatever.com DocumentRoot "/usr/local/www/apache22/data" SetEnv DJANGO_DATABASE_NAME monkeys SetEnv DJANGO_DATABASE_USER root SetEnv DJANGO_DATABASE_PASSWORD secretPass SetEnv DJANGO_DATABASE_PORT '' SetEnv DJANGO_DATABASE_HOST '' WSGIScriptAlias / /usr/local/www/apache22/data/django/wsgi_handler.py </VirtualHost> 

settings.py:

 DATABASE_NAME = os.environ.get('DJANGO_DATABASE_NAME', '') DATABASE_USER = os.environ.get('DJANGO_DATABASE_USER', '') DATABASE_PASSWORD = os.environ.get('DJANGO_DATABASE_PASSWORD', '') DATABASE_HOST = os.environ.get('DJANGO_DATABASE_HOST', '') 

This allows you to configure each site as VirtualHost in httpd.conf .

0
source

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


All Articles