0

从外部 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

4

1 回答 1

1

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

INSTALLED_APPS = (
    ...
    'shellgame.shells',
    ...
)
于 2010-10-23T01:55:08.627 回答