配置:

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'),
    },
}
DATABASE_ROUTERS = ['db_router.Router1', ]

指定app下创建指定数据库命令

python manage.py makemigraions        
python manage.py migrate app名称 --databse=配置文件数据名称的别名

手动操作:指定数据库读写分离

models.UserType.objects.using('db1').create(title='普通用户')
result = models.UserType.objects.all().using('default')

自动操作:

class Router1:
    def db_for_read(self, model, **hints):
        """
        Attempts to read auth models go to auth_db.
        """
        return 'db1'

    def db_for_write(self, model, **hints):
        """
        Attempts to write auth models go to auth_db.
        """
        return 'default'

粒度更细的情况

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'

使用:

models.UserType.objects.create(title='VVIP')

result = models.UserType.objects.all()
print(result)

 

问题:

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

# 手动操作:
m1.UserType.objects.using('default').create(title='VVIP')
m2.Users.objects.using('db1').create(name='VVIP',email='xxx')

# 自动操作:

配置:

class Router1:
    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'


DATABASE_ROUTERS = ['db_router.Router1', ]

使用:

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

 

数据库迁移时进行约束

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'

 

posted on 2018-05-17 19:57  Py行僧  阅读(201)  评论(0编辑  收藏  举报