django使用多个数据库的方法实例
程序员文章站
2022-06-23 08:14:05
通过官方文档https://docs.djangoproject.com/zh-hans/3.1/topics/db/multi-db/和csdnhttps://blog.csdn.net/songf...
通过官方文档https://docs.djangoproject.com/zh-hans/3.1/topics/db/multi-db/和csdnhttps://blog.csdn.net/songfreeman/article/details/70229839的这两篇文章可以进行多数据库的设置。但是设置后可能会出现问题,由于我连接的数据库是通过inspactdb的方法得到的model。于是在migrate的时候出现了问题,会提示 1146, “table xxx doesn't exist” 。后来发现问题可能出在路由表上,按照database_apps_mapping映射之后,django默认的表如果要写入可能会找不到数据库。而源代码里的映射关系并不包含新加入的app,例如grappelli等。
database_apps_mapping = { # example: # 'app_name':'database_name', # 'admin': 'default', # 'users': 'default', #django 'basic_estate': 'basic_estate', 'footstone': 'footstone', 'mall': 'hsmall', 'iot_biz': 'iot_biz', 'mall': 'mall', 'hsuser': 'hsuser', }
如果要解决这个问题可以修改router代码,在映射关系内找不到对应的数据库的情况下返回默认数据库连接即可:
# -*- coding: utf-8 -*- """ @author: obaby @license: (c) copyright 2013-2020, obaby@mars. @contact: root@obaby.org.cn @link: http://www.obaby.org.cn http://www.h4ck.org.cn http://www.findu.co @file: atabase_router.py.py @time: 2021/2/26 9:07 @desc: """ from django.conf import settings database_mapping = settings.database_apps_mapping class databaseappsrouter(object): """ a router to control all database operations on models for different databases. in case an app is not set in settings.database_apps_mapping, the router will fallback to the `default` database. settings example: database_apps_mapping = {'app1': 'db1', 'app2': 'db2'} """ def db_for_read(self, model, **hints): """"point all read operations to the specific database.""" if model._meta.app_label in database_mapping: return database_mapping[model._meta.app_label] return 'default' def db_for_write(self, model, **hints): """point all write operations to the specific database.""" if model._meta.app_label in database_mapping: return database_mapping[model._meta.app_label] return 'default' def allow_relation(self, obj1, obj2, **hints): """allow any relation between apps that use the same database.""" 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. """ if db in database_mapping.values(): return database_mapping.get(app_label) == db elif app_label in database_mapping: return false return none
这样django系统所需的数据库就能正常创建了:
总结
到此这篇关于django使用多个数据库的文章就介绍到这了,更多相关django多数据库内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
-
PHP的Yii框架中使用数据库的配置和SQL操作实例教程_PHP
-
php类中的$this,static,final,const,self这几个关键字使用方法_php实例
-
jQuery.holdReady(hold)的使用方法实例详解
-
YII2框架中日志的配置与使用方法实例分析
-
windwos下使用php连接oracle数据库的过程分享_php实例
-
Oracle12c使用scott等普通用户的方法实例讲解
-
Django2.2使用mysql数据库pymysql版本不匹配问题的解决过程与总结
-
twig里使用js变量的方法_php实例
-
Symfony2实现从数据库获取数据的方法小结_php实例
-
yii2中使用Active Record模式的方法_php实例