Django多数据库配置及逆向生成model

本文介绍了如何在Django项目中配置多个数据库,并详细讲解了如何从特定数据库逆向生成model到指定的app。通过修改setting.py和创建database_router.py,以及在终端中使用命令进行逆向生成,帮助开发者更好地管理和操作数据库。
摘要由CSDN通过智能技术生成

在项目中我们每个app对应不同的数据库,其中有一个是从数据库逆向生成model,做个笔记。

  1. 修改项目的setting.py配置 :
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',  # 默认用mysql
        'NAME': 'bk',                        # 数据库名 (默认与APP_ID相同)
        'USER': 'root',                        # 你的数据库user
        'PASSWORD': 'root',                        # 你的数据库password
        'HOST': 'xxx.xxx.xxx.xxx',                   # 开发的时候,使用localhost
        'PORT': '3306',                        # 默认3306
    },
    'cloudsino_test': {
            'ENGINE': 'django.db.backends.mysql',  # 默认用mysql
            'NAME': 'cloudsino_test',                        # 数据库名 (默认与APP_ID相同)
            'USER': 'root',                        # 你的数据库user
            'PASSWORD': 'root',                        # 你的数据库password
            'HOST': 'xxx.xxx.xxx.xxx',                   # 开发的时候,使用localhost
            'PORT': '3306',                        # 默认3306
        },
}

# 设置数据库的路由规则方法
DATABASE_ROUTERS = ['conf.database_router.DatabaseAppsRouter']

# 设置APP对应的数据库路由表,哪个app要连接哪个数据库,没有指定会用default那个。
DATABASE_APPS_MAPPING = {
    # example:
    #'app_name':'database_name',
    'home_application': 'cloudsino_test',
    'cmdb': 'default',
}
  1. 新建database_router.py:
    在与setting.py文件同级的目录下新建database_router.py文件:
# -*- coding: utf-8 -*-
from settings_development import DATABASE_APPS_MAPPING

DATABASE_MAPPING = DATABASE_APPS_MAPPING


class DatabaseAppsRouter(object):
    def db_for_read(self, model, **hints):
        """"建议model类型对象从哪一个数据库读取."""
        if model._meta.app_label in DATABASE_MAPPING:
            return DATABASE_MAPPING[model._meta.app_label]
        return None

    def db_for_write(self, model, **hints):
        """建议model类型对象的写入操作应该使用哪个数据库"""
        if model._meta.app_label in DATABASE_MAPPING:
            return DATABASE_MAPPING[model._meta.app_label]
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """Allow any relation between apps that use the same database.
        	如果obj1 和obj2 之间应该允许关联则返回True,如果应该防止关联则返回False,如果路由无法判断则返回None
        """
        db_obj1 = DATABASE_MAPPING.get(obj1._meta.app_label)
        db_obj2 = DATABASE_MAPPING.get(obj2._meta.app_label)
        if db_obj1 and db_obj2:
            if db_obj1 == db_obj2:
                return True
            else:
                return False
        return None

    def allow_syncdb(self, db, model):
        """Make sure that apps only appear in the related database."""

        if db in DATABASE_MAPPING.values():
            return DATABASE_MAPPING.get(model._meta.app_label) == db
        elif model._meta.app_label in DATABASE_MAPPING:
            return False
        return None

    def allow_migrate(self, db, app_label, model=None, **hints):
        """
        Make sure the auth app only appears in the 'auth_db' database.
        定义迁移操作是否允许在别名为db的数据库上运行。如果操作应该运行则返回Tru
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值