4

I have app1 and app2, I wanna app1 use db1 and app2 use db2

the approach I found is to using database router, a little bit complicated

I wanna know is there any simple way?

Can I just config it in settings.py

1
  • @IgnacioVazquez-Abrams one would want this because, you have cases when you have a separate app for hash keys which you want in a separate db to keep them private Commented May 28, 2015 at 7:54

2 Answers 2

5

No. The correct way is to use routers. It is very simple. See MyAppRouter in django's documentation: https://docs.djangoproject.com/en/dev/topics/db/multi-db/#an-example:

class MyAppRouter(object):
    """A router to control all database operations on models in
    the myapp application"""

    def db_for_read(self, model, **hints):
        "Point all operations on myapp models to 'other'"
        if model._meta.app_label == 'myapp':
            return 'other'
        return None

    def db_for_write(self, model, **hints):
        "Point all operations on myapp models to 'other'"
        if model._meta.app_label == 'myapp':
            return 'other'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        "Allow any relation if a model in myapp is involved"
        if obj1._meta.app_label == 'myapp' or obj2._meta.app_label == 'myapp':
            return True
        return None

    def allow_syncdb(self, db, model):
        "Make sure the myapp app only appears on the 'other' db"
        if db == 'other':
            return model._meta.app_label == 'myapp'
        elif model._meta.app_label == 'myapp':
            return False
        return None
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks,I will go for routers :D
4

I think the right answer is here: http://diegobz.net/2011/02/10/django-database-router-using-settings. If you have many applications and want a separate database for each, you put

DATABASE_APPS_MAPPING = {'app1':'db1', 'app2':'db2', 'app3':'db3', 'app4':'db4'}
DATABASE_ROUTERS += ['DatabaseAppsRouter']

into settings.py.

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.