2

So I'm somewhat new to the whole Django databases and maybe I just don't fully understand the Django routers talked about here: https://docs.djangoproject.com/en/dev/topics/db/multi-db/#database-routers but for the life of me I cant figure out how to link two databases together. Maybe it's because my set-up is different? The two databases are separate Django project folders and both have separate Postgre databases. I think that maybe the problem comes form them being in different folders and I'm not including the path names properly?

Here's what I have now:

settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', 
        'NAME': 'ClothesWashers',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
        },
    'RECS': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'RECS',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
        }
}

and

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'django_extensions',
    'ClothesWasher_Purchaser',
    )

and

DATABASE_ROUTERS = ['ClothesWashers.db_routers.RECS_Router',]

db_routers.py:

class RECS_Router(object):
    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'RECS_Data':
            return 'RECS'
        return 'default'

All I want to be able to do is call the RECS_Data app in the RECS database by doing something like this so that I can read from it:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'ClothesWashers.settings'
from RECS_Data.models import RecsData
g = RecsData._meta.fields

Thank you anyone who would be willing to help me!

2
  • what do you mean by "The two databases are separate Django project folders and both have separate Postgre databases" Commented May 1, 2013 at 23:54
  • I have one Django project called RECS which has an associated postgres database (called RECS) with one app called RECS_Data. I am now trying to make a completely separate Django project called ClothesWashers which also has it's own postgres database. Basically all I want to be able to do is read from the the RECS database and put data into the ClothesWashers database. Both project folders are in a folder called "Django Projects". Commented May 2, 2013 at 0:11

1 Answer 1

1

I guess you can read a secondary database with using.

Try like this.

i.e RecsData.using("RECS")._meta.fields

For more refer here https://docs.djangoproject.com/en/dev/topics/db/multi-db/#manually-selecting-a-database

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

2 Comments

Kudos for this Q&A. The main Django documentation about using "Multiple Databases" is really about using "Multiple RDBMS Instances". In other words, having fail-over or redundant data sources to poll, in some order, for reliability or load balancing. What about a single Project that needs to literally query two DATABASES? The Django application model centers around One Project, One Database, with Many Apps, Many Tables. If you are starting a Django app from scratch, sure. But if you need a GUI to help join/integrate existing databases, this "using()" feature is very important. Thanks!
@IcarusNM you are right. The use case of two complete independent databases is not covered in Django multiple databases implementation. I'm still looking for a solution to that, and I'm thinking of spliting the project in two, or use SQLAlchemy for one of the databaes.

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.