时间:2021-07-01 10:21:17 帮助过:17人阅读
1.创建数据库读写路由
- 在
meiduo_mall.utils.db_router.py
中实现读写路由
class MasterSlaveDBRouter(object):
"""数据库读写路由"""
def db_for_read(self, model, **hints):
"""读"""
return "slave"
def db_for_write(self, model, **hints):
"""写"""
return "default"
def allow_relation(self, obj1, obj2, **hints):
"""是否运行关联操作"""
return True
2.配置数据库读写路由
DATABASE_ROUTERS = [‘meiduo_mall.utils.db_router.MasterSlaveDBRouter‘]
Django实现MySQL读写分离
标签:读写 route mysql读写分离 用户名 com 数据库的配置 object efault block