django使用多个数据库的方法实例
作者:obaby@mars 时间:2024-01-21 00:11:34
通过官方文档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系统所需的数据库就能正常创建了:
来源:http://h4ck.org.cn/2021/03/django使用多个数据库
标签:django,多数据库,配置
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python安装第三方库的方法(pip/conda、easy_install、setup.py)
2022-10-28 13:22:34
如何实现全文检索?
2010-05-24 18:24:00
给JavaScript自定义一个Trim函数
2008-04-20 16:30:00
Python中base64与xml取值结合问题
2021-08-22 21:15:03
深入理解python中sort()与sorted()的区别
2021-08-17 11:46:49
Python中的集合一起来学习一下
2022-05-01 05:06:59
![](https://img.aspxhome.com/file/2023/8/81838_0s.png)
python实现K折交叉验证
2023-06-08 18:49:01
![](https://img.aspxhome.com/file/2023/2/127012_0s.jpg)
Php header()函数语法及使用代码
2023-09-04 13:32:20
基于Python词云分析政府工作报告关键词
2022-12-23 12:01:21
![](https://img.aspxhome.com/file/2023/1/131091_0s.jpg)
全面了解构造函数继承关键apply call
2024-04-16 10:36:07
JavaScript随机打乱数组顺序之随机洗牌算法
2024-05-03 15:33:00
python3爬取torrent种子链接实例
2022-03-05 11:23:44
![](https://img.aspxhome.com/file/2023/2/99162_0s.jpg)
python中xlutils库用法浅析
2023-06-05 15:22:26
Python编程使用tkinter模块实现计算器软件完整代码示例
2022-07-17 06:31:07
![](https://img.aspxhome.com/file/2023/5/95595_0s.jpg)
利用Python复制文件的9种方法总结
2022-06-25 15:00:27
![](https://img.aspxhome.com/file/2023/6/70656_0s.png)
使用Python paramiko模块利用多线程实现ssh并发执行操作
2021-02-02 03:10:05
Python ini文件常用操作方法解析
2022-01-07 09:52:44
ASP.NET MVC Bundles 用法和说明(打包javascript和css)
2023-07-17 01:20:59
分享Sql Server 存储过程使用方法
2024-01-13 06:42:36
Js 随机数产生6位数字
2024-05-02 17:31:03