python - 如何在 Django 中迁移多个数据库?
问题描述
我正在用 Django 做一个项目,数据太多了,所以我需要形成不同的数据库来存储它们。我为每个应用程序制作了一个数据库路由器以匹配唯一的数据库,但是当我迁移它们时,所有的表都迁移到了同一个数据库,这里是代码:
from django.conf import settings
DATABASE_MAPPING = settings.DATABASES_APPS_MAPPING
class DatebaseAppsRouter(object):
"""
该类为连接不同数据库的路由,在setting中通过设置DATABASE_MAPPING来匹配数据库
例:
DATABASE_APPS_MAAPING = {"app1":"db1", "app2":"db2"}
"""
def db_for_read(self, model, **kwargs):
"""将所有读操作指向指定的数据库"""
if model._meta.app_label in DATABASE_MAPPING:
return DATABASE_MAPPING[model._meta.app_label]
return None
def db_for_write(self, model, **kwargs):
"""将所有写操作指向指定的数据库"""
if model._meta.app_label in DATABASE_MAPPING:
return DATABASE_MAPPING[model._meta.app_label]
return None
def allow_relation(self, obj1, obj2, **kwargs):
"""允许使用相同数据库的应用程序之间的任何关系"""
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
else:
return None
def allow_syncdb(self, db, model):
"""确保这些应用程序只出现在相关的数据库中"""
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, **kwargs):
"""确保身份验证应用程序只出现在login数据库中"""
if db in DATABASE_MAPPING.values():
return DATABASE_MAPPING.get(app_label) == db
elif app_label in DATABASE_MAPPING:
return False
return None
通过使用路由器,我想迁移多个数据库,但它不起作用
System check identified some issues:
WARNINGS:
?: (urls.W001) Your URL pattern '^login/$' uses include with a route ending with a '$'. Remove the dollar from the route to avoid problems including URLs.
?: (urls.W001) Your URL pattern '^main/$' uses include with a route ending with a '$'. Remove the dollar from the route to avoid problems including URLs.
?: (urls.W005) URL namespace 'admin' isn't unique. You may not be able to reverse all URLs in this namespace
Operations to perform:
Apply all migrations: Themepark, admin, auth, contenttypes, ginseng_holding_hotel, holiday_resort, login, nature_scenic_area, sessions, the_scent_spot_trusteeship, tourist_communications, travel_agency
Running migrations:
No migrations to apply.
那么如何解决这个问题呢?谢谢!
解决方案
我认为问题出在你的 urls.py
如果您正在使用
django.urls.path()
,则不必添加胡萝卜符号和美元符号并这样做。
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', views.login, name='login'),
path('main/', views.main, name='main'),
]
但如果你正在使用,
django.urls.url()
那么你应该这样做
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^login/', views.login, name='login'),
url(r'^main/', views.main, name='main'),
]
如果你不介意,你能告诉我们你的 urls.py 吗?
推荐阅读
- swift - RxSwift,如何更温和地使用 NotificationCenter?
- python - 如何在列表和字符串中约会约定?
- python - GeoTIFF 数据文件中的纬度/经度(公里)与计算的距离不匹配
- python - apk python 包未安装在 Alpine Docker 映像中
- node.js - 如何避免 NodeJs 应用程序中的重复文件和文本?
- model - 我如何评估 SpaCy 再训练模型的准确性?
- apache-spark - Hive 时间戳格式?
- javascript - 如何接收 Twilio statusCallback
- javascript - 为什么 JSON.parse("foo") 失败但 JSON.parse(' "foo" ') 成功?
- regex - 我的正则表达式搜索字符串有什么问题?