读写分离

在settings中配置不同名称的数据库连接参数,并配置一条数据库选择路由

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'db1': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db1.sqlite3'),
},
}

(1)第一种方法:

    手动选择要使用的数据库

m1.UserType.objects.using('default').create(title='VVIP')
m2.Users.objects.using('db1').create(name='VVIP',email='xxx')

(2)方法二:

    定义一下路由类,自动执行数据类

在seetings 中加入一条配置

DATABASE_ROUTERS = ['db_router.Router1',]
class Router1:
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth_db.
"""
if model._meta.model_name == 'usertype':
return 'db1'
else:
return 'default' def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
return 'default'

为读写操作指定类

这样在执行查询和修改时候就无需指定数据库

多应用分库

创建数据库时候执行指定的命令

          app01中的表在default数据库创建
app02中的表在db1数据库创建 # 第一步:
python manage.py makemigraions # 第二步:
app01中的表在default数据库创建
python manage.py migrate app01 --database=default # 第三步:
app02中的表在db1数据库创建
python manage.py migrate app02 --database=db1

对数据库迁移和读写操作进行约束

数据库迁移时进行约束:
class Router1:
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
All non-auth models end up in this pool.
"""
if db=='db1' and app_label == 'app02':
return True
elif db == 'default' and app_label == 'app01':
return True
else:
return False # 如果返回None,那么表示交给后续的router,如果后续没有router,则相当于返回True def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth_db.
"""
if model._meta.app_label == 'app01':
return 'default'
else:
return 'db1' def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
if model._meta.app_label == 'app01':
return 'default'
else:
return 'db1'

django数据库读写分离,分库的更多相关文章

  1. Django 数据库读写分离 分库分表

    多个数据库 配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BA ...

  2. django数据库读写分离

    django数据库读写分离 1. 配置数据库 settings.py文件中 用SQLite: DATABASES = { 'default': { 'ENGINE': 'django.db.backe ...

  3. Django的数据库读写分离

    Django的数据库读写分离 1.首先是配置数据库 在settings.py文件中增加多个数据库的配置: DATABASES = { 'default': { 'ENGINE': 'django.db ...

  4. 学会数据库读写分离、分表分库——用Mycat,这一篇就够了!

    系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理本身优化也是非常重要的.主从.热备.分表分库等都是系统发展迟早会遇到的技术问题问题.Mycat是一 ...

  5. 学会数据库读写分离、分表分库——用Mycat

    系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理本身优化也是非常重要的.主从.热备.分表分库等都是系统发展迟早会遇到的技术问题问题.Mycat是一 ...

  6. (转)学会数据库读写分离、分表分库——用Mycat,这一篇就够了!

    原文:https://www.cnblogs.com/joylee/p/7513038.html 系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理 ...

  7. 数据库读写分离、分表分库——用Mycat

    转:     https://www.cnblogs.com/joylee/p/7513038.html 系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据 ...

  8. Django----配置数据库读写分离

    Django配置数据库读写分离 https://blog.csdn.net/Ayhan_huang/article/details/78784486 https://blog.csdn.net/ayh ...

  9. spring+mybatis利用interceptor(plugin)兑现数据库读写分离

    使用spring的动态路由实现数据库负载均衡 系统中存在的多台服务器是"地位相当"的,不过,同一时间他们都处于活动(Active)状态,处于负载均衡等因素考虑,数据访问请求需要在这 ...

随机推荐

  1. js 常用类型转换简写

    1.字符串转数字 +'666' 2.转换为字符串 ''+666 //'666'

  2. 【Python】学习笔记十:字典

    字典是Python中唯一的映射类型(哈希表) 字典的对象时可变的,但字典的键值必须是用不可变对象,并且一个字典中可以使用不同类型的键值 1.定义字典 dict={key1:value1,key2:va ...

  3. jconsole JDK1.6 使用手册 (转)

    转载出处 文章作者:hornet 本文地址:http://hornetblog.sinaapp.com/?p=5 英文版地址: http://download.oracle.com/javase/6/ ...

  4. ubuntu14.04使用root用户登录桌面,ubuntu14.04root 转

    ubuntu安装好之后,默认是不能用root用户登录桌面的,只能使用普通用户或者访客登录.怎样开启root用户登录桌面呢? 先用普通用户登录,然后切换到root用户,然后执行如下命令: vi /usr ...

  5. WebDev.WebServer.exe,IIS ,IIS Express

    调试ASP.NET程序的服务器有三种WebDev.WebServer.exe,IIS ,IIS Express,以下是从网上整理的他们各自的优缺点,记录以备查阅 1.ASP.NET开发服务器--Cas ...

  6. 浅谈ThreadPool 线程池(引用)

    出自:http://www.cnblogs.com/xugang/archive/2010/04/20/1716042.html 浅谈ThreadPool 线程池 相关概念: 线程池可以看做容纳线程的 ...

  7. DS18B20 crc 算法

    http://blog.csdn.net/pengrui18/article/details/24740973 https://www.maximintegrated.com/cn/app-notes ...

  8. 消息队列 概念 配合SpringBoot使用Demo

    转http://www.jianshu.com/p/048e954dab40 概念: 分布式消息队列 ‘分布式消息队列’包含两个概念 一是‘消息队列’,二是‘分布式’ 那么就先看下消息队列的概念,和为 ...

  9. Xcode 警告信息处理:Format string is not a string literal (potentially insecure)

    转自:http://www.oschina.net/question/54100_33881 NSObject *obj = @"A string or other object." ...

  10. Swift中UIView类方法(animateWithDuration)的使用

    需求:利用Swift语言实现OC语言中UIView类方法 [UIView animateWithDuration:0.5 animations:^{ bgView.alpha= 1; }]; 在Swi ...