4721

Django - Multiple Databases

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:

<strong>settings.py:</strong>

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',]

<strong>db_routers.py:</strong>

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!

Answer1:

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

Recommend

  • Syntax error when trying to import database from two PostgreSQL databases
  • Is it possiable to create a mySQL database for android app within flutter application and use that c
  • Batch file to delete all folders in a directory except the newest folder
  • Xamarin iOS debugger not hitting breakpoints
  • Git cleanup/garbage collection on remote VSO git repository
  • Visual Studio - PathTooLongException even when the path length is less than 260 characters
  • Lua: Line breaks in strings
  • Does Apportable support to build library binary (.a/.so)?
  • Is there a way to save the selected text and highlight it again once the page is refreshed?
  • Suppressing passwd when calling sqlplus from shell script
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • Installed module is empty
  • How to get Eclipse Oxygen to run on Java 9
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • D3 nodes and links from JSON with nested arrays of children
  • Switching to Release Build causes runtime error in Web Reference
  • jQuery .attr() and value
  • Azure Cloud Service Web Role web pages do not load
  • Fetching methods from BroadcastReceiver to update UI
  • Bug in WPF DataGrid
  • TFS: Get latest causes slow project reloading
  • Controls, properties, events and timers running in design time
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Running a C# exe file
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • Symfony2: How to get request parameter
  • ORA-29908: missing primary invocation for ancillary operator
  • QuartzCore.framework for Mono Develop
  • XCode can't find symbols for a specific iOS library/framework project
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Revoking OAuth Access Token Results in 404 Not Found
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How to set the response of a form post action to a iframe source?
  • Unable to use reactive element in my shiny app
  • Converting MP3 duration time