Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Reply my own question...</p> <p>The only way to get this right is make one middleware to get database name and work with locals, like this:</p> <p>File: middleware.py</p> <pre><code>from threading import local from django.contrib.sessions.models import Session from django.contrib.auth.models import User from web_core.models import UserProfile my_local_global = local() class CustomerMiddleware(object): def process_request(self, request): my_local_global.database_name = get_database_name(request) def get_database_name(request): session_key = request.session.session_key try: session = Session.objects.get(session_key=session_key) uid = session.get_decoded().get('_auth_user_id') user = User.objects.get(pk=uid) profile = UserProfile.objects.get(pk=uid) if profile: return profile.dbname else: return None except: return None </code></pre> <p>after this, add <strong>middleware.py</strong> in your <strong>settings.py</strong>:</p> <pre><code>MIDDLEWARE_CLASSES = ( (..) 'middleware.CustomerMiddleware', ) </code></pre> <p>to finish, make one more file to get db router:</p> <p>File: authrouter:</p> <pre><code>class PadraoRouter(object): def db_for_read(self, model, **hints): from middleware import my_local_global return my_local_global.database_name def db_for_write(self, model, **hints): from middleware import my_local_global return my_local_global.database_name def allow_relation(self, obj1, obj2, **hints): return None def allow_syncdb(self, db, model): return True class AuthRouter(object): def db_for_read(self, model, **hints): if model._meta.app_label == 'auth': return 'auth_db' if model._meta.app_label == 'sessions': return 'auth_db' if model._meta.app_label == 'web_core': return 'auth_db' return None def db_for_write(self, model, **hints): if model._meta.app_label == 'auth': return 'auth_db' if model._meta.app_label == 'sessions': return 'auth_db' if model._meta.app_label == 'web_core': return 'auth_db' return None def allow_relation(self, obj1, obj2, **hints): if obj1._meta.app_label == 'auth' or\ obj2._meta.app_label == 'auth': return True if obj1._meta.app_label == 'sessions' or\ obj2._meta.app_label == 'sessions': return True if obj1._meta.app_label == 'web_core' or\ obj2._meta.app_label == 'web_core': return True return None def allow_syncdb(self, db, model): if db == 'auth_db': return model._meta.app_label == 'auth' elif model._meta.app_label == 'auth': return False return None </code></pre> <p>NOTE: I need to put <strong>import</strong> in each def, because Django 1.5.1 has a bug, if you put import into top of file.. cycle imports..</p> <p>after this, change again your <strong>settings.py</strong> to add the router:</p> <pre><code>DATABASE_ROUTERS = ['authrouter.AuthRouter', 'authrouter.PadraoRouter'] </code></pre> <p><strong>Remember</strong></p> <p>I make this way, because I have one database, only for auth.. each user can access a different database, depending what is save in dbname field.</p> <p>If you have other solution, please let's me know!</p> <p>Thanks to everybody.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload