【问题标题】:Using Django ORM outside of Django throws ImportError on assigning to the FK在 Django 之外使用 Django ORM 会在分配给 FK 时引发 ImportError
【发布时间】:2010-10-23 00:40:42
【问题描述】:

当从外部 shell 使用 Django ORM 时,使用具有 ForeignKey 字段的模型在分配给它时会引发 ImportError。

这是我从 ./manage.py shell 和普通 python shell 中的 shell 会话的粘贴。

bradyrj@machine:~/workspaces/django/shellgame$ python manage.py shell
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from shells.models import Location, Shell
>>> okinawa = Location()
>>> okinawa.title = "Okinawa"
>>> okinawa.description = "Island south of main Japanese Islands, in the Ryukyu Island chain."
>>> okinawa.save()
>>> s = Shell()
>>> s.title = "Conch"
>>> s.description = "it's just a friggin shell"
>>> s.location_found = okinawa
>>> s.save()
>>>
[1]+ Stopped python manage.py shell


bradyrj@machine:~/workspaces/django/shellgame$ cd ../
bradyrj@machine:~/workspaces/django$ cd ../
bradyrj@machine:~/workspaces$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import sys
>>> os.environ["DJANGO_SETTINGS_MODULE"] = 'shellgame.settings'
>>> sys.path.append('/home/bradyrj/workspaces/django')
>>> from shellgame.shells.models import Shell, Location
>>> nc = Location()
>>> nc.title = "Pine Knoll Shores"
>>> nc.description = "best beaches"
>>> nc.save()
>>> s = Shell()
>>> s.title = "shark tooth"
>>> s.description = "old, small, arrowhead-like"
>>> s.location_found = nc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/related.py", line 354, in __set__
val = getattr(value, self.field.rel.get_related_field().attname)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/related.py", line 758, in get_related_field
data = self.to._meta.get_field_by_name(self.field_name)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/options.py", line 291, in get_field_by_name
cache = self.init_name_map()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/options.py", line 321, in init_name_map
for f, model in self.get_all_related_m2m_objects_with_model():
File "/usr/local/lib/python2.6/dist-packages/django/db/models/options.py", line 396, in get_all_related_m2m_objects_with_model
cache = self._fill_related_many_to_many_cache()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/options.py", line 410, in _fill_related_many_to_many_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 shells
>>> 

这是一个包含项目源代码的仓库:https://bitbucket.org/ryanbrady/shellgame/src

【问题讨论】:

  • 首先,分配FK字段时抛出异常,而不是保存时。其次,shellgame 显然在路径中丢失了。为什么不直接在 Django shell 中检查 sys.path 并在自己的 Python shell 中重现它?
  • django 项目目录和直接目录都在路径上。整个django项目中的每个目录都需要在路径上吗?
  • @RJBrady:在 Django shell 中,您可以从 shells.models 导入。所以我认为你的 Python shell 的路径中只有 /home/bradyrj/workspaces/django/shellgame 丢失。
  • @RJBrady:您还可以使用来自django.core.management 的函数setup_environ(在__init__.py 中)。这就是manage.py 的有效作用。
  • 我之前确实尝试过 setup_environ ,但它仍然有同样的错误。它确实将应用程序目录添加到路径中,但只有将“project.appname”添加到已安装的应用程序中才会有任何不同。

标签: django orm shell django-models importerror


【解决方案1】:

这可能会在 settings.py 中修复它:

INSTALLED_APPS = (
    ...
    'shellgame.shells',
    ...
)

【讨论】:

  • 太棒了。这通常也是 Linux 上的一个问题 :)
猜你喜欢
  • 2018-01-17
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
  • 2010-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多