django使用2个不同的数据库 [英] django use 2 different databases

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

问题描述

我想使用django制作仪表板.问题是,我想要一个数据库用于数据(将显示在仪表板上)和一个数据库用于Django操作(Django创建的所有自动表)我看到了此答案,但是当我键入 manage.py migration --database时= data_db_name ,然后django也在该DB上创建其所有自动表,我不想要那样.我只希望该数据库与要在仪表板上显示的数据保持一致.

有可能吗?如果是这样,我该怎么办?

谢谢

解决方案

在这种情况下,最好的方法是定义多个数据库并使用Django的一项功能,即称为数据库路由

完整文档此处

简而言之:

在settings.py中定义如下多个数据库:

  DATABASES = {'默认': {},'读': {'NAME':'auth_db_name','ENGINE':'django.db.backends.mysql','USER':'mysql_user','PASSWORD':'swordfish',},'wrie':{'NAME':'primary_name','ENGINE':'django.db.backends.mysql','USER':'mysql_user','PASSWORD':'垃圾邮件',}} 

无需定义自己的路由器:

  Class AuthRouter:"路由器,用于控制模型中所有模型上的所有数据库操作auth和contenttypes应用程序."route_app_labels = {'auth','contenttypes'}def db_for_read(self,model,** hints):"尝试读取auth和contenttypes模型进入auth_db."如果model._meta.app_label在self.route_app_labels中:返回读"不返回def db_for_write(self,model,** hints):"尝试编写auth和contenttypes模型进入auth_db."如果model._meta.app_label在self.route_app_labels中:返回写"不返回def allow_relation(self,obj1,obj2,**提示):"如果auth或contenttypes应用程序中的模型为涉及."如果 (self.route_app_labels中的obj1._meta.app_label或self.route_app_labels中的obj2._meta.app_label):返回True不返回def allow_migrate(self,db,app_label,model_name = None,**提示):"确保auth和contenttypes应用仅出现在'auth_db'数据库."如果app.label在self.route_app_labels中:return db =='read'不返回 

settings.py 中定义djando需要遵循的路由器

  DATABASE_ROUTERS = ['path.to.ReadRouter','path.to.WriteRouter'] 

I want to use django to make a dashboard. The thing is, I want to have one DB for data (that will be displayed on the dashboard) and one DB for Django operations (all the automatic tables Django creates) I saw this answer but when I type manage.py migrate --database=data_db_name then django creates all its automatic table also on that DB, and I dont want that. I only want this DB to be with the data to be displayed on the dashboard.

Is it possible? if so, how can I do it?

Thanks

解决方案

The best approach, in this case, is to define multiple databases and use a feature by Django that is called database routing

Full documentation here

In few words:

In settings.py define multiple databases like this:

DATABASES = {
    'default': {},
    'read': {
        'NAME': 'auth_db_name',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'mysql_user',
        'PASSWORD': 'swordfish',
    },
    'wrie': {
        'NAME': 'primary_name',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'mysql_user',
        'PASSWORD': 'spam',
    }
}

Than you have to define you own router:

class AuthRouter:
"""
A router to control all database operations on models in the
auth and contenttypes applications.
"""
route_app_labels = {'auth', 'contenttypes'}

def db_for_read(self, model, **hints):
    """
    Attempts to read auth and contenttypes models go to auth_db.
    """
    if model._meta.app_label in self.route_app_labels:
        return 'read'
    return None

def db_for_write(self, model, **hints):
    """
    Attempts to write auth and contenttypes models go to auth_db.
    """
    if model._meta.app_label in self.route_app_labels:
        return 'write'
    return None

def allow_relation(self, obj1, obj2, **hints):
    """
    Allow relations if a model in the auth or contenttypes apps is
    involved.
    """
    if (
        obj1._meta.app_label in self.route_app_labels or
        obj2._meta.app_label in self.route_app_labels
    ):
       return True
    return None

def allow_migrate(self, db, app_label, model_name=None, **hints):
    """
    Make sure the auth and contenttypes apps only appear in the
    'auth_db' database.
    """
    if app_label in self.route_app_labels:
        return db == 'read'
    return None

Than in the settings.py define the routers that djando needs to follow

DATABASE_ROUTERS = ['path.to.ReadRouter', 'path.to.WriteRouter']

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

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