Django multidb:写入多个数据库 [英] Django multidb: write to multiple databases

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

问题描述

使用Django multidb,写一个路由器运行主/从基础架构。但是可以将写入的路由器写入多个数据库吗?我的用例是一个项目的集合,所有项目都运行在同一个域上。要保存用户注册/登录每个站点,我想同步 contrib.auth contrib.sessions 表。是否可以使用Django multidb或者应该查看数据库系统的复制功能(在我的情况下是MySQL)?

With Django multidb, it's fairly easy to write a router that runs a master/slave infrastructure. But is it possible to write a router that writes to multiple databases? My use case is a collection of projects, all running on the same domain. To save users from registering/login in on every site, I'd like to synchronize the contrib.auth and contrib.sessions tables. Is that possible with Django multidb or should I look into replication features of the database system (MySQL in my case)?

推荐答案

我现在正在处理Django分片模式。

I'm working on a Django sharding schema right now.

我看着Django路由器,但决定自己滚动。

i looked at the Django router but decided to roll my own.

有关您的问题的一些想法:

some thoughts on your issue:


  1. 一个想法是使用一个数据库然后在保存后通过使用Django信号复制适当的数据。

像 -

import settings.databases as dbs_list

def post_save_function(UserModel):
     for db in dbs_list:
          UserModel.save(using=db,force_insert=True)

保存用户对象(至少在单数据库模型)似乎通过django.contrib内部发生的各种魔法来保存会话,验证等数据,所以有机会你可能不必去了解所有这些的名称和类型数据库表。

saving User objects (at least on a single-DB model) seems to save session, auth, etc. data under the hood via the various magic occurring inside django.contrib, so there's a chance you might not have to go in and figure out the names and types of all these database tables.

为了支持这种工作的可能性,我发誓我最近读过某个地方(可能在Alex Gaynor的一篇博文中),如果一个对象有一个外部关键Django将尝试使用对象所在的同一个DB(对于obv Django通常运行的方式的原因)。

in support of the possibility of this working, I swear I read somewhere recently (probably on one of Alex Gaynor's blog posts) that if an object has a foreign key Django will attempt to use the same DB the object lives on (for obvious reasons in light of the way Django typically operates).


  1. 另一个想法:

例如在您引用的Django multiDB页面上,我想知道是否可以使用以下内容:

from the example on the Django multiDB page you referenced I wonder if something like the following would work:

他们的示例代码:

def allow_syncdb(self, db, model):
        "Explicitly put all models on all databases."
        return True

可能的修改:

def allow_syncdb(self,db,model):

def allow_syncdb(self, db, model):

    if isinstance(model,User):
         return True

    elif isinstance(model,Session):
         return True

    else:
         ''' something appropriate --
             whatever your sharding schema is for other objects '''

再次看着它,这个代码可能比db_for_write函数更有用。但是你得到这个想法。

looking at it again, this code would probably be more useful as the "db_for_write" function. but you get the idea.

毫无疑问,你必须添加其他类型的模型才能使这项工作(所有的auth的东西,这是广泛的)。

there are no doubt other types of model you would have to add to make this work (all the auth stuff, which is extensive).

祝你好运!希望这在某种程度上是有帮助的。

good luck! hope this is helpful in some way.

我对你的发现和评论感兴趣

i'm interested in your findings and comments!

jb

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

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