Django - 多个数据库 [英] Django - Multiple Databases

查看:169
本文介绍了Django - 多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对整个Django数据库来说有些新鲜事,也许我完全不了解Django路由器这里所说的话:
https://docs.djangoproject.com/en/dev/topics/db/multi-db/#database-routers
但是对于我来说,我无法弄清楚如何将两个数据库链接在一起。也许是因为我的设置是不同的?这两个数据库是单独的Django项目文件夹,并且都有单独的Postgre数据库。我认为这可能是因为它们在不同的文件夹中,而且我没有正确地包含路径名称?



这是我现在所在:



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':'',
}
}

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

  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'

要做的就是通过这样做来调用RECS数据库中的RECS_Data应用程序,以便我可以从中读取:

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

感谢你愿意帮助我的任何人!

解决方案

我想你可以使用二进制数据库阅读。



尝试这样。

  ie RecsData.using(RECS)._ meta .fields 

有关更多信息,请参阅这里 https://docs.djangoproject.com/en/dev/topics/db/multi-db/#手动选择数据库


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!

解决方案

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

这篇关于Django - 多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆