在 Django 之外使用 Django ORM 在分配给 FK 时会抛出 ImportError

发布于 2024-09-28 13:41:42 字数 3450 浏览 1 评论 0原文

当从外部 shell 使用 Django ORM 时,使用带有foreignkey字段的模型,在分配给它时会抛出 ImportError 。

这是我的 shell 会话的粘贴,来自 ./manage.py shell 和普通的 python 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< /a>

When using the Django ORM from an external shell, using a model with a ForeignKey field an ImportError is thrown when assigning to it.

And here is a paste of my shell sessions both from ./manage.py shell and from normal python 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
>>> 

Here's a repo with the source code with the project: https://bitbucket.org/ryanbrady/shellgame/src

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴骨ら 2024-10-05 13:41:42

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

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

This may fix it in settings.py:

INSTALLED_APPS = (
    ...
    'shellgame.shells',
    ...
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文