0

I have a multi-database Django Project (Python 2.7, Django 1.4 ) and I need that the database used in every single transaction triggered in a request be selected through the url from that request.

Ex:

url='db1.myproject.com'

Would trigger the use of 'db1' in every transaction, like this:

MyModel.objects.using('db1').all()

That is where my problem resides. Its virtualy impossible include in every call of 'Model.objects' the '.using(dbname)' function (this project already have 2 dozen of models, and its build with scaling in mind, so it could have hundreds of models, and almost everymodel has (or might have) a implementation a of post_save/post_delete that acts as database triggers).

Is there a way of telling django that he should change the default database for this or that request ?

I already have a Custom QuerySet and a Custom Manager, so, if i could tell THEM the database should be this or that could do the trick to (i dont find anywhere how to get request data inside them).

Any help would be apreciated.

lealhugui
  • 189
  • 1
  • 12

1 Answers1

0

I think my answer to a similar question here will do what you need: https://stackoverflow.com/a/21382032/202168

...although I'm a bit worried you may have chosen an unconventional or 'wrong' way of doing database load balancing, maybe this article can help with some better ideas:
http://engineering.hackerearth.com/2013/10/07/scaling-database-with-django-and-haproxy/

Community
  • 1
  • 1
Anentropic
  • 32,188
  • 12
  • 99
  • 147
  • it just seems a bit odd to be selecting your database based on the request url – Anentropic Jun 13 '14 at 14:28
  • I Wouldn't build or scale databases differently from what the article sugested. The need of multiple databases comes from the fact that the project hosts business apps, so we work with a "per client" database. It provides WAY more encapsulation, and its easier when you need to dump a "one client only" data – lealhugui Jun 13 '14 at 14:35