Django項目配置多個數據庫
阿新 • • 發佈:2019-02-12
ocs users top urn turn new 其他 gopro 個數
給一個項目配置多個數據庫,不同app裏的model對應不同的數據庫
settings中的設置
1 # 設置3個數據庫 2 3 DATABASES = { 4 ‘users_base‘: { 5 ‘ENGINE‘: ‘django.db.backends.mysql‘, 6 "HOST": "127.0.0.1", 7 "NAME": "thunder_blended_learning", 8 "PASSWORD": "root", 9 "USER": "root",10 "PORT": 3306, 11 ‘OPTIONS‘: {‘charset‘: ‘utf8mb4‘}, 12 }, 13 ‘cms‘: { 14 ‘ENGINE‘: ‘django.db.backends.mysql‘, 15 "HOST": "127.0.0.1", 16 "NAME": "thunder_blended_learning", 17 "PASSWORD": "root", 18 "USER": "root", 19 "PORT": 3306, 20 ‘OPTIONS‘: {‘charset‘: ‘utf8mb4‘}, 21 }, 22 ‘default‘: { 23 ‘ENGINE‘: ‘django.db.backends.mysql‘, 24 "HOST": "127.0.0.1", 25 "NAME": "xuetangx", 26 "PASSWORD": "root", 27 "USER": "root", 28 "PORT": 3306, 29 ‘OPTIONS‘: {‘charset‘: ‘utf8mb4‘}, 30 } 31 } 32 33 # 不同app可能對應不同的db,註意在本站app: cms,users_base,users_up不允許被migrate 34 DATABASE_ROUTERS = [‘new_xuetangx.app_db_router.UsersBaseDBRouter‘,‘new_xuetangx.app_db_router.CmsDBRouter‘]
app_db_router文件中設置不同的app對應的數據庫的名字,沒有指定數據庫的app用默認的default數據庫
1 class UsersBaseDBRouter(object): 2 """ 3 A router to control all database operations on models in the 4 auth application. 5 """ 6 def db_for_read(self, model, **hints): 7 """ 8 Attempts to read auth models go to auth_db. 9 """ 10 if model._meta.app_label == ‘users_base‘: 11 return ‘users_base‘ 12 if model._meta.app_label == ‘auth‘: 13 return ‘users_base‘ 14 return None 15 16 def db_for_write(self, model, **hints): 17 """ 18 Attempts to write auth models go to auth_db. 19 """ 20 if model._meta.app_label == ‘users_base‘: 21 return ‘users_base‘ 22 if model._meta.app_label == ‘auth‘: 23 return ‘users_base‘ 24 return None 25 26 def allow_relation(self, obj1, obj2, **hints): 27 """ 28 Allow relations if a model in the auth app is involved. 29 """ 30 if obj1._meta.app_label == ‘users_base‘ or 31 obj2._meta.app_label == ‘users_base‘: 32 return True 33 if obj1._meta.app_label == ‘auth‘ or 34 obj2._meta.app_label == ‘auth‘: 35 return True 36 return None 37 def allow_migrate(self, db, app_label, model_name=None, **hints): 38 """ 39 do not allow migrate to users_base‘s db. 40 """ 41 if db == ‘users_base‘ or app_label == ‘users_base‘ or app_label == ‘auth‘: 42 return False 43 else: 44 return None 45 46 47 class CmsDBRouter(object): 48 """ 49 A router to control all database operations on models in the 50 auth application. 51 """ 52 def db_for_read(self, model, **hints): 53 """ 54 Attempts to read auth models go to auth_db. 55 """ 56 if model._meta.app_label == ‘cms‘: 57 return ‘cms‘ 58 return None 59 60 def db_for_write(self, model, **hints): 61 """ 62 Attempts to write auth models go to auth_db. 63 """ 64 if model._meta.app_label == ‘cms‘: 65 return ‘cms‘ 66 return None 67 68 def allow_relation(self, obj1, obj2, **hints): 69 """ 70 Allow relations if a model in the auth app is involved. 71 """ 72 if obj1._meta.app_label == ‘cms‘ or 73 obj2._meta.app_label == ‘cms‘: 74 return True 75 return None 76 def allow_migrate(self, db, app_label, model_name=None, **hints): 77 """ 78 do not allow migrate to cms‘s db. 79 """ 80 if db == ‘cms‘ or app_label == ‘cms‘: 81 return False 82 else: 83 return None
上述設置表示,app:auth , users_base 使用users_base 數據庫,可讀可寫,可以在app內進行表關聯操作,不可以進行migrate操作
app:cms 使用cms 數據庫,可讀可寫,可以在app內進行表關聯操作,不可以進行migrate操作
其他的app使用default 數據庫
1 class LmsDBRouter(object): 2 """ 3 A router to control all database operations on models in the 4 auth application. 5 """ 6 def db_for_read(self, model, **hints): 7 """ 8 Attempts to read auth models go to auth_db. 9 """ 10 if model._meta.app_label == ‘lms‘: 11 return ‘default‘ 12 return None 13 14 def db_for_write(self, model, **hints): 15 """ 16 Attempts to write auth models go to auth_db. 17 """ 18 if model._meta.app_label == ‘lms‘: 19 return ‘default‘ 20 return None 21 22 def allow_relation(self, obj1, obj2, **hints): 23 """ 24 Allow relations if a model in the auth app is involved. 25 """ 26 if obj1._meta.app_label == ‘lms‘ or 27 obj2._meta.app_label == ‘lms‘: 28 return True 29 return None 30 def allow_migrate(self, db, app_label, model_name=None, **hints): 31 """ 32 can only migrate lms app. Other app are not allowed to be migrated here 33 """ 34 if app_label == ‘lms‘: 35 return db == ‘default‘ 36 return False
上述設置表示,app:lms 使用 default 數據庫,可讀可寫,可以在app內進行表關聯操作,可以進行migrate操作
另外若不寫上述lms的配置,也能起到相應的作用,再次寫出僅供學習之用
參考官網:https://docs.djangoproject.com/en/2.1/topics/db/multi-db/
Django項目配置多個數據庫