开发者

Dynamically select database based on request

I'm trying to keep my RESTful site DRY, and I can't come up with a good way to factor out the code to dynami开发者_JAVA百科cally select from each "user's" separate database. We've got a separate database for each client. This comes in as a part of the URL, and is passed into each view as a keyword arg. I want to give each and every view the behavior of accessing the corresponding database WITHOUT have to make sure each programmer writing a view remembers to use

Thing.objects.using(user).all()

and

t = Thing()
t.save(using=user)

every time. It seems like there ought to be some way to intercept the request and set the default database based on the args to the view before it hits the view, allowing us to use the usual

Thing.objects.all()

This would also have the advantage of factoring out all the user resolution code into a more appropriate place.


We do this by the following technique.

  1. Apache picks off the first part of the path and routes this to a specific mod_wsgi Daemon.

  2. Each mod_wsgi daemon is a different customer's installation.

We have many parallel customers, each with (nearly) identical code, all based off a single common installation of the base software.

Each customer has a separate settings.py with their unique configuration.

They don't (actually can't) know about each other because Apache has peeled off the top layer of the path for us.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜