django配置多数据库#
1.多数据库
在django项目中可能需要多个数据库,在使用多个数据库时需要做一些额外的配置。官网上写的比较详细,可以参考,此文只做大致介绍。
2.配置多个数据库
在setting.py文件中,需要配置好多个数据库,如:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'abc',
'USER': 'abc',
'PASSWORD': '123456',
'HOST': '192.168.100.100',
'PORT': 3306,
},
'auth_db': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'auth_db',
'USER': 'def',
'PASSWORD': '123456',
'HOST': '192.168.100.101',
'PORT': 3306,
}
}
3.使用多个数据库
-
使用数据库路由
-
编写Database Router
class AuthRouter: """ A router to control all database operations on models in the auth application. """ def db_for_read(self, model, **hints): """ Suggest the database that should be used for read operations for objects of type model. Attempts to read auth models go to auth_db. """ if model._meta.app_label == 'auth': return 'auth_db' return None def db_for_write(self, model, **hints): """ Suggest the database that should be used for writes of objects of type Model. Attempts to write auth models go to auth_db. """ if model._meta.app_label == 'auth': return 'auth_db' return None def allow_relation(self, obj1, obj2, **hints): """ Return True if a relation between obj1 and obj2 should be allowed, False if the relation should be prevented, or None if the router has no opinion. This is purely a validation operation, used by foreign key and many to many operations to determine if a relation should be allowed between two objects. If no router has an opinion (i.e. all routers return None), only relations within the same database are allowed. Allow relations if a model in the auth app is involved. """ if obj1._meta.app_label == 'auth' or \ obj2._meta.app_label == 'auth': return True return None def allow_migrate(self, db, app_label, model_name=None, **hints): """ Determine if the migration operation is allowed to run on the database with alias db. Return True if the operation should run, False if it shouldn’t run, or None if the router has no opinion. Make sure the auth app only appears in the 'auth_db' database. """ if app_label == 'auth': return db == 'auth_db' return None
这四个方法是django提供用于路由选择的函数,每个函数的含义在上述代码注释中有说明(感觉英文更容易解释)。
-
在settings.py文件中配置DB router,例如:
DATABASE_ROUTERS = ['auth.db_router.AuthRouter']
配置routers路径,是指python能够找到的可导入model路径
-
在model中指定app_label
其中db_table表示model对应的表名称,不采用默认的表名。例如:
class Products(models.Model): ''' @note: sycn from bugzilla ''' id = models.IntegerField(primary_key=True) name = models.CharField(unique=True, max_length=64) class Meta: app_label = 'auth' db_table = 'products'
这样设置后,每次访问model时会根据路由策略来访问到特定的数据库中。
-
-
手动指定数据库
手动指定数据库是指在查询或者保存数据时,指定特定的数据库,手动指定方式的优先级高于路由配置。
-
为一个查询集手动选择一个数据库
你可以在 查询集 “链”中的任何点为“查询集”选择数据库。我们通过在 查询集 上调用 using() 来得到使用指定数据库的另一个 查询集 。例如:
>>> # This will run on the 'default' database. >>> Author.objects.all() >>> # So will this. >>> Author.objects.using('default').all() >>> # This will run on the 'other' database. >>> Author.objects.using('other').all()
-
为 save() 选择一个数据库
在使用 Model.save() 时加上 using 关键字可以指定保存到哪个数据库。
例如,要把一个对象保存到 legacy_users 数据库应该这样做:
>>> my_object.save(using='legacy_users')
-
参考文档: