views:

36

answers:

1

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

+1  A: 

This may fix it in settings.py:

INSTALLED_APPS = (
    ...
    'shellgame.shells',
    ...
)
sunn0
it worked on OSX.
RJBrady
great. it is usually a problem on Linux as well :)
sunn0