import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = "trade.settings"
from trade.turkey.models import *
d = DemoRecs.objects.all()
d.delete()
当我运行它时,如果我遗漏d.delete()
行,它会导入正常。这条线上的错误。为什么?如果我发表评论,一切都很酷。我可以插入。我可以更新。但当我拥有那条线时,一切都搞定了。
追溯是:
d.delete()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 447, in delete
obj._collect_sub_objects(seen_objs)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py", line 585, in _collect_sub_objects
for related in self._meta.get_all_related_objects():
File "/usr/local/lib/python2.6/dist-packages/django/db/models/options.py", line 347, in get_all_related_objects
self._fill_related_objects_cache()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/options.py", line 374, in _fill_related_objects_cache
for klass in get_models():
File "/usr/local/lib/python2.6/dist-packages/django/db/models/loading.py", line 167, in get_models
self._populate()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/loading.py", line 61, in _populate
self.load_app(app_name, True)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/loading.py", line 76, in load_app
app_module = import_module(app_name)
File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
ImportError: No module named turkey
答案 0 :(得分:4)
trade
中缺少sys.path
项目的目录。
答案 1 :(得分:2)
尝试将“trade”添加到pythonpath ...
import os.path
_pypath = os.path.realpath(os.path.dirname(__file__) + '/trade')
sys.path.append(_pypath)