Rajeev
Rajeev

Reputation: 46899

Django multiple database mapping

In Django when using multiple DB's how to map databases, for ex: from request there is a parameter as app1 which maps to db1 and another request app2 mapping to db2. Now how to choose db's much before sending the request to views

DATABASE_APPS_MAPPING = {'app1':'db1','app2':'db2'}


DATABASES = {
  'default': {
  },
  'db1': {
      'ENGINE': 'django.db.backends.mysql',
      'NAME': 'hp1',
      'USER': 'server',
      'PASSWORD': 'hpdata',
      'HOST': '192.168.3.11',
      'PORT': '3306'
  },
  'db2': {
      'ENGINE': 'django.db.backends.mysql',
      'NAME': 'hp2',
      'USER': 'server',
      'PASSWORD': 'hpdata',
      'HOST': '192.168.3.11',
      'PORT': '3306'
  }
}

Upvotes: 3

Views: 2370

Answers (2)

Let's say I can import a function that gives me the database:

project/app/views.py:

from .utils import which_db, item_search
from django.shortcuts import render
from .models import SomeModel

def some_view(request):
    obj = SomeModel.objects.using(which_db(request)).filter(**item_search(request))
    return render(request, "some_model.html", {'object':obj}

It's a bother having to append the using every time. There is a way to route similar queries to different databases (documented here in more detail).

project/settings.py:

DATABASE_ROUTERS = ['project.db_routes.AppRouter',]
DB_ROUTING = {'app1':'db1','app2':'db2'}

project/db_routes.py:

from django.conf import settings


class AppRouter:
    def __init__(self):
        self.DB = settings.DB_ROUTING

    def db_for_read(self, model, **hints):
        return self.DB.get(self.model.app_label, 'default')

    def db_for_write(self, model, **hints):
        return self.DB.get(self.model.app_label, 'default')

    def allow_relation(self, obj1, obj2, **hints):
        return True

    def allow_migrate(self, db, app_label, model=None, **hints):
        return self.DB.get(app_label, None)

I'm assuming you want to be able to have foreignkeys between the databases. If not, return False from allow_relation. allow_migrate insures objects only exists in their proper database.

I have not tested this solution, but it does conform to the documentation.

Upvotes: 3

user1797792
user1797792

Reputation: 1189

I'm not entirely sure what you want to do. But my guess is that you'll find your answer here:

https://docs.djangoproject.com/en/1.8/topics/db/multi-db/#topics-db-multi-db-routing

If you really want to select a database based on the url (subdomain e.a.) than you will have to write a custom Middleware class:

https://docs.djangoproject.com/en/1.8/topics/http/middleware/

Upvotes: 0

Related Questions