1

Could anyone be so kind to explain me what is meant in the documentation: https://docs.djangoproject.com/en/1.7/topics/db/multi-db/#topics-db-multi-db-hints

I've added a new db in myproject/settings.py

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'testdb',
    'USER': 'test',
  },
'db2': {
    'ENGINE': 'mysql.connector.django',
    'NAME': 'db2a',
}   }

but this command work only if I replace default with db2:

python manage.py inspectdb > models.py

now that I get the models from db2, how can I read from them swithching back db2 and default?

1) if I try a makemigrations, it see all new models but then it try to crate them, and I need only to read them because they already exists...

Now we’ll need to handle routing. First we want a router that knows to send queries for the auth app to auth_db

2) In which file should be added the AuthRouter(object): ? I can add a file called routers.py next to settings.py?

 from django.conf import settings

class db_Router(object):
    def db_for_read(self, model, **hints):
        return 'db2'

    def db_for_write(self, model, **hints):
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        db_list = ('default', 'db2')
        if obj1._state.db in db_list and obj2._state.db in db_list:
            return True
        return None

    def allow_migrate(self, db, model):
        return True

    def allow_syncdb(self, db, model):
        if db == 'db2':
            return model._meta.app_label == 'appofdb2'
        elif model._meta.app_label == 'appofdb2':
            return False
        return None

this should work? or I'm completely out of way?

1 Answer 1

2

Just answer myself:

1)python manage.py inspectdb --database=db2 > models.py

2) create a routers.py file in root folder of the project

Everything worked like a charm!

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.