database.py
1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
Database
https://docs.djangoproject.com/en/1.11/ref/settings/#databases
"""
# import pymysql
from django.db.models import options
# pymysql.install_as_MySQLdb()
# 同一App下的Model可能会存储到不同的db,所以根据app_label来做db_router不是特别合适
# 所以添加situ_db_label来映射model到db
# 为Django的model的Meta添加额外的属性situ_db_label
options.DEFAULT_NAMES = tuple(list(options.DEFAULT_NAMES) + ['situ_db_label'])
# 数据库连接池配置
# MYSQLPOOL_ARGUMENTS = {
# 'recycle': 30,
# 'pool_size': 64,
# 'max_overflow': 10,
# 'timeout': 5,
# 'use_threadlocal': False,
# }
class DBRouter(object):
def db_for_read(self, model, **hints):
if hasattr(model._meta, 'situ_db_label'):
return model._meta.situ_db_label
return None
def db_for_write(self, model, **hints):
if hasattr(model._meta, 'situ_db_label'):
return model._meta.situ_db_label
return None
def allow_relation(self, obj1, obj2, **hints):
return True