gpt4 book ai didi

django 1.4 数据库路由器 - "cannot import name connection"

转载 作者:行者123 更新时间:2023-12-04 18:47:39 25 4
gpt4 key购买 nike

我正在尝试在 django 上对我的数据库进行分片,但是在第一步中出现了奇怪的错误。

我做了一个简单的数据库路由器,什么都没有:

'''file /myproject/myapp/routers.py'''
class ShardingRouter(object):

def db_for_read(self, model, **hints):
return 'default'

def db_for_write(self, model, **hints):
return 'default'

def allow_relation(self, obj1, obj2, **hints):
return 'default'

def allow_syncdb(self, db, model):
return 'default'

我在settings.py中添加:
DATABASE_ROUTERS = ['myproject.myapp.routers.ShardingRouter',]

我收到此错误:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 219, in __call__
self.load_middleware()
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 45, in load_middleware
mod = import_module(mw_module)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/hosting/myproject/myproject/middleware.py", line 10, in <module>
from django.contrib.sites.models import Site
File "/usr/local/lib/python2.7/dist-packages/django/contrib/sites/models.py", line 1, in <module>
from django.db import models
File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 16, in <module>
router = ConnectionRouter(settings.DATABASE_ROUTERS)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 116, in __init__
raise ImproperlyConfigured('Error importing database router %s: "%s"' % (klass_name, e))
ImproperlyConfigured: Error importing database router ShardingRouter: "cannot import name connection"

什么“联系”?这是什么意思?
找不到,问题出在哪里((

最佳答案

您需要导入 connections在您的 settings.py :

from django.db import connections

...
...

DATABASE_ROUTERS = ['myproject.myapp.routers.ShardingRouter',]
...
...

关于django 1.4 数据库路由器 - "cannot import name connection",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11711536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com