`
zhouxi2010
  • 浏览: 50089 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

django中south支持多数据库

阅读更多
settings的DATABASES配置成多个,其中一个是default,其他的用appname来做key;
然后指明一个route文件,来做数据库路由
 DATABASE_ROUTERS = [
      'meila_admin.route.Router',
 ]
再来实现这个route.py
from django.db import DEFAULT_DB_ALIAS
appnames = ['**', '**'] #这里是用非默认数据库的app
class Router(object):
    """
    A router to control all database operations on models in the
    auth application.
    """
    def db_for_read(self, model, **hints):
        """
        Attempts to read auth models go to auth_db.
        """
        if model._meta.app_label in appnames:
            return model._meta.app_label
        return None
 
    def db_for_write(self, model, **hints):
        """
        Attempts to write auth models go to auth_db.
        """
        if model._meta.app_label in appnames:
            return model._meta.app_label
        return None
 
    def allow_relation(self, obj1, obj2, **hints):
        return True
 
    def allow_syncdb(self, db, model):
        """
        Make sure the auth app only appears in the 'auth_db'
        database.
        """
        if model._meta.app_label in ['south']:
            return True
        if db in appnames:
            return model._meta.app_label == db
        elif db == DEFAULT_DB_ALIAS and model._meta.app_label not in appnames:
            return True
        return False
 
 
#注意第一次south其他数据库时,需要用syncdb
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics