DDR爱好者之家 Design By 杰米

通过官方文档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使用多个数据库的方法实例

总结

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。