0

I would like to use two databases in my application:

  • local database
  • external database

in the first one I want to save all django tables , e.g auth_group

To do this I tried to use router class, but unsuccessfully - it doesn't work. Below you can find my code

Django 1.11

setings file:

DATABASE_ROUTERS = ['mainApp.models.DefaultDBRouter','subfolder.newApp.models.testDBRouter',]

models.py - main app - I want to use default DB for this model

from __future__ import unicode_literals

from django.db import models

class list_a( models.Model ):
    region = models.CharField(max_length=50, verbose_name="Region")
    country = models.CharField(max_length=50, verbose_name="Country")

    def __unicode__(self):
        return str( self.country )

class DefaultDBRouter(object):
    """
    A router to control all database operations on models in the
    auth application.
    """

    def db_for_read(self, model, **hints):
        """
        Reads go to a default.
        """
        return "default"

    def db_for_write(self, model, **hints):
        """
        Writes always go to default.
        """
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        """
        Relations between objects are allowed if both objects are
        in the default.
        """
        db_list = ('default')
        if obj1._state.db in db_list and obj2._state.db in db_list:
            return True
        return None

    def allow_migrate(self, db, app_label, model=None, **hints):
        """
        All non-micro-management models end up in this pool.
        """
        return True

models.py - test app - I want to use second db for this model

class testTable( models.Model ):

    date = models.DateField(verbose_name="Date")
    number_name = models.CharField(max_length=50, verbose_name="Number name")

    def __unicode__(self):
        return str( self.number_name )


ELIGIBLE_APPS = [
   'subfolder.newApp',
]


class testDBRouter(object):
    def db_for_read(self, model, **hints):
        print model._meta.app_label
        if model._meta.app_label in ELIGIBLE_APPS:
            return 'secondDB'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in ELIGIBLE_APPS:
            return 'secondDB'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if obj1._meta.app_label in ELIGIBLE_APPS or \
                obj2._meta.app_label in ELIGIBLE_APPS:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in ELIGIBLE_APPS:
            return db == 'secondDB'
        return None

And if I execute the following command:

python2.7 manage.py migrate --database=secondDB

Then django creates all tables from both models files and standard django tables inside that database ( should create only one )

1 Answer 1

1

I use two db and I do it in the following way and I do the normal migration . python manage.py migrate

 DATABASES = {


'default': {
   'ENGINE': 'django.db.backends.mysql',
   'NAME': 'xxxx',
   'USER': 'root',
   'PASSWORD': '',
   'HOST': '',
   'PORT': '',

 },
 'base2': {
       'ENGINE': 'django.db.backends.oracle',
       'NAME': 'xxxxxx',
       'USER': '',
       'PASSWORD': '',
   },

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.