Separating the database read Django
1, the first is the configuration database
The settings.py
increase configure multiple database files:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'db2': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db2.sqlite3'), # 指定数据库名
}
}
Use mysql
configurations:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': 3306,
'USER': 'user',
'PASSWORD': 'pd',
'NAME': 'db_read'
},
'db2': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': 8306,
'USER': 'root',
'PASSWORD': 'pd',
'NAME': 'db_write'
}
}
2, perform database migration
python manage.py migrate -- database db2 # 将default库中的表结构同步到db2
After the completion of the implementation of the above instructions, you will find, db2 library which will have our table
3, take a look at the case of read and write separation, django how to play
- Manually separate read and write
In view function, which is specified by the library objects.using ( "library name") Use
from django.shortcuts import HttpResponse
from . import models
def write(request):
models.Products.objects.using('default').create(name='大猿', age=12)
return HttpResponse('写入成功')
def read(request):
obj = models.Products.objects.using('db2').filter(id=1).first()
return HttpResponse(obj.name)
- Automatic separate read and write
Rewrite create a py file, where we create a router.py
file
class Router:
# 读操作用default库,就return这个库名字符串
def db_for_read(self,model,**kwargs): # db_for_read固定写法,读操作自动匹配 # db_for_write固定写法,写操作自动匹配
return 'default'
# 读操作用db2库,就return这个库名字符串
def db_for_write(self,model,**kwargs): # db_for_write固定写法,写操作自动匹配
return 'db2'
Also you need to settings.py
perform configuration file:
DATABASE_ROUTERS = ['app01.router.Router',]
#写上面这个类的路径,我的是在app01应用文件夹下面的router.py文件中了
After the automatic setting operation does not need the use of library functions in the logical view ofusing
def dbtest(request):
data = []
# 向db2数据库中写入数据
models.Class.objects.create(name='大猿',age=18)
# 从db1中读取数据
data = models.Class.objects.all()
return render(request,'dbtest.html',{'data':data})
4, a multi-master program from
A master multi-slave of time, that is, when reading from multiple databases, we can do this: create a file or write function below the
import random
class Router:
def db_for_read(self, model, **kwargs):
return random.choices(['db1', 'db2', 'db3'])
#多个库读的时候,可以简单的写个随机选择
def db_for_write(self, model, **kwargs):
return 'db'
In the settings.py
same configuration and the top file:
DATABASE_ROUTERS = ['app01.router.Router',]
#写上面这个类的路径,我的是在app01应用文件夹下面的router.py文件中了
5, sub-library sub-table
When we need to make a different app operation of different databases, such as app01
database operation db1
, app02
database operation db2
, how should we do it, or create a separate file, worded as follows:
class Router:
def db_for_read(self, model, **kwargs):
if model._meta.app_label == 'app01':
return 'db1'
if model._meta.app_label == 'app02':
return 'db2'
def db_for_write(self, model, **kwargs):
if model._meta.app_label == 'app01':
return 'db1'
if model._meta.app_label == 'app02':
return 'db2'
# model._meta.app_label 获取当前函数所在app的app名称
# 如果是类,需要将类名加上
# model.UserInfo._meta.app_label 获取该类所在app的app名称
In the settings.py
file configuration:
DATABASE_ROUTERS = ['app01.router.Router',]
#写上面这个类的路径,我的是在app01应用文件夹下面的router.py文件中了