【发布时间】:2018-10-01 16:56:48
【问题描述】:
有没有办法使用connections 连接到 django 测试数据库?
我试过了
cursor = connections['test_name_of_main_db'].cursor()
并且还在设置中指定了测试数据库名称,但我仍然收到错误:
Traceback (most recent call last):
File "/home/tets/4d/lib/python3.5/site-packages/django/db/utils.py", line 176, in ensure_defaults
conn = self.databases[alias]
KeyError: 'auto_tests'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/tets/healthware_server/component/medical/tests.py", line 72, in test_model_observation
cursor = connections['auto_tests'].cursor()
File "/home/tets/4d/lib/python3.5/site-packages/django/db/utils.py", line 208, in __getitem__ self.ensure_defaults(alias)
File "/home/tets/4d/lib/python3.5/site-packages/django/db/utils.py", line 178, in ensure_defaults
raise ConnectionDoesNotExist("The connection %s doesn't exist" % alias)
django.db.utils.ConnectionDoesNotExist: The connection auto_tests doesn't exist
【问题讨论】:
-
您想在测试期间或测试之前/之后连接?
-
durng(在setUp方法中会很好),我只需要创建对象而不是从signals.py中分离信号(不想评论信号)
标签: django django-database django-tests