问题描述
Django:1.3;身体伤害:1.5.3
Django: 1.3; PyCharm: 1.5.3
我正在为使用GEOS保存Point对象的Django应用程序编写单元测试.对于本地测试,我遵循了根据GeoDjango文档自定义Spatialite后端的所有步骤.
I am writing unit-tests for a Django application that uses GEOS to save Point objects. For local testing, I followed every step in customizing a Spatialite backend according to the GeoDjango documentation.
每当我尝试使用Point对象创建和保存模型实例时,我都遇到了GEOS_ERROR(GEOS_ERROR:几何必须为Point或LineString).很明显,确实在模型的get_or_create函数中传递了Point对象.而且可以在外壳中保存相同的模型而没有问题.
I ran into a GEOS_ERROR (GEOS_ERROR: Geometry must be a Point or LineString) whenever I tried to create and save a model instance with a Point object. It was clear that a Point object was indeed passed in the get_or_create function for the model. And the same model can be saved without a problem in shell.
在没有太多代码导致此错误的情况下,我发现每次运行单元测试时,Django都会导入设置,创建测试数据库并立即销毁数据库,然后再次为该数据库创建测试数据库.最终会引发错误的测试.
Without getting too much into the code that caused this error, I found that every time I run a unit test, Django imported the settings, created a test database and destroyed the database immediately, and then created the test database again for the tests that ultimately would throw errors.
Testing started at 5:09 PM ...<br />
Importing Django settings module settings
SpatiaLite version ..: 2.4.0 Supported Extensions:
- 'VirtualShape' [direct Shapefile access]
- 'VirtualText'[direct CSV/TXT access]
- 'VirtualNetwork [Dijkstra shortest path]
- 'RTree' [Spatial Index - R*Tree]
- 'MbrCache' [Spatial Index - MBR cache]
- 'VirtualFDO' [FDO-OGR interoperability]
- 'SpatiaLite' [Spatial SQL - OGC]
PROJ.4 Rel. 4.7.1, 23 September 2009
GEOS version 3.3.0-CAPI-1.7.0Creating test database 'default'...
Destroying old test database 'default'...
SpatiaLite version ..: 2.4.0 Supported Extensions:
(same Spatialite settings again)
cannot start a transaction within a transaction
cannot rollback transaction - SQL statements in progress
Syncing...
Creating tables ...
我怀疑这是由PyCharm引起的.但是,当我从Terminal shell运行'python manage.py test'时,重复了相同的过程,并抛出了相同的错误.
I suspected this was caused by PyCharm. But when I run 'python manage.py test' from a Terminal shell, the same process was repeated and the same errors were thrown.
我检查了设置文件,但找不到为什么提示自己两次导入,以及为什么两次创建了测试数据库.用于创建Spatialite数据库的必需init_spatialite-2.*.sql也位于项目路径上.
I checked my settings file but couldn't find out why it is prompting itself to be imported twice and why the test database was created twice. The required init_spatialite-2.*.sql for creating Spatialite database is also on the project path.
任何建议将不胜感激!
更新:JetBrains通知我,可以使用此修补程序或runserver --noreload修复在runserver期间两次导入settings.py的问题. http://code.djangoproject.com/changeset/15911
Update:I was informed by JetBrains that the twice-importing of settings.py during runserver can be fixed with this patch or by runserver --noreload.http://code.djangoproject.com/changeset/15911
但是,对于测试任务,导入错误仍然存在.
However, the import error still persisted for test tasks.
推荐答案
这很可能是由于导入路径上同时包含settings.py和包含包而引起的.由于可以将设置导入到不同的模块(设置和myproject.settings)下,因此可以执行两次.
This is caused most probably because you have both the settings.py and containing package on the import path. Because the settings can be imported under different modules (settings and myproject.settings) it can get executed two times.
只需确保始终使用DJANGO_SETTINGS_MODULE = settings而不是DJANGO_SETTINGS_MODULE = myproject.settings或从包含settings.py的目录中删除__init__.py文件
Just make sure you always use DJANGO_SETTINGS_MODULE=settings instead of DJANGO_SETTINGS_MODULE=myproject.settings or remove the __init__.py file from the directory that contains settings.py
或者更好地,升级到django 1.4
Or, better, upgrade to django 1.4
这篇关于为什么在运行单元测试时,两次导入django设置,并两次创建测试数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!