Django - Runtime database switching -


in work want run server multiple databases. databases switching should occur when acces url http://myapp.webpage.com or http://other.webpage.com. want run 1 server instance , @ moment of http request switch database , return corresponding response.

we've been looking mantainable , 'django-friendly' solution. in our investigation have found possible ways this, have not enough information about.

option 1: django middleware

the django middleware runs each time server receive http request.

  1. making database switch here best option using django database routers far know allow change database model or group or models.

  2. another option set django model manager instance in middleware , force models re-assign objects attribute added attribute in custom middleware.

  3. my last option create new attribute in request object received middleware return database alias settings.py , in each model query use using method.

option 2: class-based view mixin

create mixin use past 3 options, the mixin must set in class-based views. if programmer forget set mixin , comes production server, data (or stop being) in right database, , don't wanna take risk.

option 3: changing database settings in runtime

this option works is not recommended , risky.

update:

how works?

middlewares.py

import django.conf conf import os.path   class selectdb(object):      def process_request(self, request):         print request.meta['http_referer']         file_database = open("booklog/database.txt", "r")         database = file_database.read(10)         file_database.close()         if database != 'default':             conf.settings.databases['default']['name'] = database 

any information solve appreciated.

answer (it worked me)

the question answered here, in stackoverflow. i'd love functionality in django. bit hard find way make possible.

i think important comment great work wilduck made django plugin django-dynamic-db-router, it's great plugin makes possible operation in (a bit) different way.

thanks lot @jl peyret , @ire_and_curses.

and answer @ire_and_curses. @ least in moment, in project i'm working it's need. in previous projects needed similar behavior , made 1 server per instance terrible mantain , update each server, automating process.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -