网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么在django中运行多个数据库-创新互联

本篇文章给大家分享的是有关怎么在django中运行多个数据库,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

留坝网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。创新互联2013年至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联
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: /tupian/20230522/
  http://www.h5ck.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中运行多个数据库-创新互联
文章起源:http://bjjierui.cn/article/decdhs.html

其他资讯