Django项目配置多个数据库

给一个项目配置多个数据库,不同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/

猜你喜欢

转载自www.cnblogs.com/liuguniang/p/10364534.html