django使用多個(gè)數(shù)據(jù)庫的方法實(shí)例
通過官方文檔https://docs.djangoproject.com/zh-hans/3.1/topics/db/multi-db/和csdnhttps://blog.csdn.net/songfreeman/article/details/70229839的這兩篇文章可以進(jìn)行多數(shù)據(jù)庫的設(shè)置。但是設(shè)置后可能會(huì)出現(xiàn)問題,由于我連接的數(shù)據(jù)庫是通過inspactdb的方法得到的model。于是在migrate的時(shí)候出現(xiàn)了問題,會(huì)提示 1146, “Table xxx doesn't exist” 。后來發(fā)現(xiàn)問題可能出在路由表上,按照DATABASE_APPS_MAPPING映射之后,django默認(rèn)的表如果要寫入可能會(huì)找不到數(shù)據(jù)庫。而源代碼里的映射關(guān)系并不包含新加入的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', }
如果要解決這個(gè)問題可以修改router代碼,在映射關(guān)系內(nèi)找不到對(duì)應(yīng)的數(shù)據(jù)庫的情況下返回默認(rèn)數(shù)據(jù)庫連接即可:
# -*- 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系統(tǒng)所需的數(shù)據(jù)庫就能正常創(chuàng)建了:
總結(jié)
到此這篇關(guān)于django使用多個(gè)數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)django多數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Django ForeignKey與數(shù)據(jù)庫的FOREIGN KEY約束詳解
- pycharm中django框架連接mysql數(shù)據(jù)庫的方法
- django inspectdb 操作已有數(shù)據(jù)庫數(shù)據(jù)的使用步驟
- Python web框架(django,flask)實(shí)現(xiàn)mysql數(shù)據(jù)庫讀寫分離的示例
- Django連接本地mysql數(shù)據(jù)庫(pycharm)的步驟
- django 解決model中類寫不到數(shù)據(jù)庫中,數(shù)據(jù)庫無此字段的問題
- Python的Django框架實(shí)現(xiàn)數(shù)據(jù)庫查詢(不返回QuerySet的方法)
- django 數(shù)據(jù)庫返回queryset實(shí)現(xiàn)封裝為字典
- Django項(xiàng)目如何給數(shù)據(jù)庫添加約束
相關(guān)文章
OpenCV+python3實(shí)現(xiàn)視頻分解成圖片
這篇文章主要為大家詳細(xì)介紹了OpenCV+python3實(shí)現(xiàn)視頻分解成圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09一起解密Python中的*args和**kwargs無限可能的函數(shù)參數(shù)
這篇文章主要來跟大家一起解密Python中的*args和**kwargs無限可能的函數(shù)參數(shù)使用的靈活性,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06python 轉(zhuǎn)換 Javascript %u 字符串為python unicode的代碼
這篇文章主要介紹了python 轉(zhuǎn)換 Javascript %u 字符串為python unicode的代碼,需要的朋友可以參考下2016-09-09Django之 ArrayAgg與Admin技巧學(xué)習(xí)
這篇文章主要為大家介紹了Django之ArrayAgg與Admin的技巧學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Python爬蟲獲取數(shù)據(jù)保存到數(shù)據(jù)庫中的超詳細(xì)教程(一看就會(huì))
使用爬蟲爬數(shù)據(jù),總要涉及到數(shù)據(jù)持久化,也就是數(shù)據(jù)存儲(chǔ)的問題,下面這篇文章主要給大家介紹了關(guān)于Python爬蟲獲取數(shù)據(jù)保存到數(shù)據(jù)庫中的超詳細(xì)教程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06Django關(guān)于admin的使用技巧和知識(shí)點(diǎn)
在本篇文章里小編給大家整理的是關(guān)于Django的admin簡單使用的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們可以跟著學(xué)習(xí)下。2020-02-02解決selenium模塊利用performance獲取network日志請求報(bào)錯(cuò)的問題(親測有效)
這篇文章主要介紹了解決selenium模塊利用performance獲取network日志請求報(bào)錯(cuò)的問題(親測有效),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03如何用python識(shí)別滑塊驗(yàn)證碼中的缺口
這篇文章主要介紹了如何用python識(shí)別滑塊中的缺口,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04