While playing around, I found myself wandering down this path, 
wondering if anybody else had been here before.

In summary, I'm wondering if there's an easy way to jockey my DB 
settings based on information completely self-contained within 
the request.

In longer detail...

In this case, it was the subdomain, so one might have

   customer1.example.com -> customer1_db
   customer2.example.com -> customer2_db

but it would translate fairly well to a top-level target like

   example.com/customer1/ -> customer1_db
   example.com/customer2/ -> customer2_db

I don't know enough yet about the internals of Django's DB 
connecting-calls and the timing involved there.

One option is to have a separate settings.py for every customer, 
but as customers are added, this grows unwieldy especially as the 
code base remains the same.  Ideally, only a handful of Django 
processes would be needed to maintain as many DBs have been 
established.

My first idea at a hack involved a middleware that poked at the 
django.settings module but this is a dangerous sort of sport if I 
rely on it and the core devs aren't expecting crazy folks to do 
something like this.  From digging at the code a little, it 
appears that the DB-connection is made on first request via a 
call to cursor(), and that connection cached for the lifetime of 
the DatabaseWrapper object (which seems to be indefinitely the 
lifespan of the Django/Python code itself).  Thus, monkeying with 
the django.settings file wouldn't work predictably unless I also 
monkeyed with the db.connection.connection, something like


   if db.connection.connection is not None:
     # manage transaction(s) here?
     db.connection.connection.close()
     db.connection.connection = None
   settings.DATABASE_NAME = get_db_name_from_request(request)

Do any developers better versed in the internals of Django see 
any obvious flaws in this line of reasoning?  Other than the 
one-request-means-one-connect overhead where currently the 
connection-overhead is only incurred once per Django-process-launch.

Appreciating your input,

-tim




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to