On Saturday, March 3, 2012 12:31:34 PM UTC+2, João Dias de Carvalho Neto 
wrote:
>
> Hi
>
> I'm recently diving into Django framework and a doubt is flashing my head.
>
> If I want to develop and to deploy cloud web applications, each customer 
> will have your singular database. In the Django approach, The database 
> configurations are in the settings.py file. How can I do to separate a 
> settings.py file for each customer, and their unique configurations without 
> create a copy of my entire project?
>
> It's possible to create dinamically a settings.py file at user session or 
> it's better keep those files existing at web root?
>
> Any help will be apreciated?
>
> There are probably some applications out there which does this for you.

However, do not try to dynamically change settings in production. That is 
likely to cause you problems. It might work if you know what you are doing 
and you are running in single-threaded environment. Then again, it might 
not.

I suggest you just setup each client to different url in your HTTP server 
config. Then, each user can have own settings file, containing essentially:
from general_settings import *
DATABASES['default']['name'] = 'users_db_name'
DATABASES['default']['user'] = 'user'
...

I think that is the easiest way. Of course, if you have a lot of different 
clients who use the service rarely this will not work too well as each of 
those will need one process. In that case I suggest you create a middleware 
which does the database rerouting for you.

There are two options for the middleware: you could assign the user's 
DBWrapper to django.db.connections['default']. This relies on 
implementation details of Django, and does not work in 1.3. In 1.4 beta 
this should work. See the source code of django/db for details. Another 
option is to use threading.local() to store the database alias for the 
user. Then, when doing queries set .using(db_from_threading_local) for each 
query. Maybe database routers could also be used for this purpose together 
with threading.local. I don't know that are too well.

 - Anssi

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/wUoQSYfr8nIJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to