Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django, how to access request object in settings.py

Tags:

django

Is it somehow possible to access the request object inside settings.py? Maybe by creating a temporary settings object, modifying it and then telling the rest of the "chain" to use that instead of the normal settings.py?

I need it to decide which DB-connection to use.

As an extra question. If I were to have something like 5000 database connections, would settings.py be just as efficient as storing them in a sqlite db on the web-frontend? And would it be just as painless to update the connections? Or does the server have to be reloaded to catch the changes in settings.py?

Edit: To clarify why I might be needing that many connections. I am building a webapp. It's SaaS and like many others the accounts will each have a subdomain that they can create users on and will have no need to interact with any other subdomain/account. It would then be nice to confine each account to a DB all of its own. This grants some extra security and simplifies the app. There are many more advantages to it, but this should illustrate it just fine. This is why I might end up with that many different databases (but not that many different physical servers if that makes any difference).

like image 964
Mathias Nielsen Avatar asked Feb 22 '10 01:02

Mathias Nielsen


2 Answers

If i understand this right, you could use django's new db-routing system and select database on-the-fly based on model instance (e.g. your user) without the need of using() call.

like image 81
Dmitry Shevchenko Avatar answered Sep 23 '22 22:09

Dmitry Shevchenko


Just adding this for anyone else looking for the same. It is not currently possible. I have created a feature request on the Django bug-tracker (#13056 i think) and submitted a prototype for a fix, but I don't think it will be included anytime soon and it probably has a lot of bugs in it.

I have moved the project to Flask as it has the g object that is perfectly suited for this.

like image 20
Mathias Nielsen Avatar answered Sep 21 '22 22:09

Mathias Nielsen