Django多租户项目连接不同数据库

参考资料:https://blog.csdn.net/bocai_xiaodaidai/article/details/119276263
tips:

数据库migrate迁移命令需要重写,因为默认的migrate拿到不tenant_id
以下是重写之后的migrate命令,使用时指定要迁移的数据库即可。如:

python manage.py makemigrations

python manage.py migrate --database  tenant_1   (仅迁移tenant_1数据库)

python manage.py migrate --database  all    (all代表迁移所有数据库)
# -*- coding:utf-8 -*-
from django.conf import settings
from django.core.management.commands.migrate import Command as DjangoCommand
 
from itom_cmdb.libs.utils.tenant_util import TenantUtil
 
 
class Command(DjangoCommand):
 
    def add_arguments(self, parser):
        parser.add_argument(
            '--database',
            required=True,
            help='Nominates a database to synchronize, Defaults to all databases.',
        )
        parser.add_argument(
            '--skip-checks', action='store_true',
            help='Skip system checks.',
        )
        parser.add_argument(
            'app_label', nargs='?',
            help='App label of an application to synchronize the state.',
        )
        parser.add_argument(
            'migration_name', nargs='?',
            help='Database state will be brought to the state after that '
                 'migration. Use the name "zero" to unapply all migrations.',
        )
        parser.add_argument(
            '--noinput', '--no-input', action='store_false', dest='interactive',
            help='Tells Django to NOT prompt the user for input of any kind.',
        )
        parser.add_argument(
            '--fake', action='store_true',
            help='Mark migrations as run without actually running them.',
        )
        parser.add_argument(
            '--fake-initial', action='store_true',
            help='Detect if tables already exist and fake-apply initial migrations if so. Make sure '
                 'that the current database schema matches your initial migration before using this '
                 'flag. Django will only check for an existing table name.',
        )
        parser.add_argument(
            '--plan', action='store_true',
            help='Shows a list of the migration actions that will be performed.',
        )
        parser.add_argument(
            '--run-syncdb', action='store_true',
            help='Creates tables for apps without migrations.',
        )
        parser.add_argument(
            '--check', action='store_true', dest='check_unapplied',
            help='Exits with a non-zero status if unapplied migrations exist.',
        )
 
    def handle(self, *args, **options):
        database = options['database']
        if database == 'all':
            self.stdout.write(self.style.SUCCESS("Nominates all database to synchronize:"))
            for d in settings.DATABASES:
                self.stdout.write(self.style.SUCCESS(f'Sync {d}:'))
                TenantUtil.set_current_tenant(d)
                options['database'] = d
                super().handle(*args, **options)
                TenantUtil.release_current_tenant()
        else:
            TenantUtil.set_current_tenant(database)
            super(Command, self).handle(*args, **options)
            TenantUtil.release_current_tenant()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值