2010-10-23 13 views
0

Lors de l'utilisation de l'ORM Django à partir d'un shell externe, une erreur ImportError est générée lors de l'affectation à un modèle avec un champ ForeignKey.L'utilisation de Django ORM en dehors de Django déclenche ImportError lors de l'attribution au FK

Et voici une pâte de mes sessions shell à la fois depuis ./manage.py shell et à partir de shell python normal.

[email protected]:~/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 


[email protected]:~/workspaces/django/shellgame$ cd ../ 
[email protected]:~/workspaces/django$ cd ../ 
[email protected]:~/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 
>>> 

est ici une prise en pension avec le code source du projet: https://bitbucket.org/ryanbrady/shellgame/src

+0

En premier lieu, l'exception est levée lorsque vous attribuez un champ FK pas quand vous économisez Deuxièmement, 'shellgame' est évidemment absent dans le chemin. Pourquoi n'inspectez-vous pas simplement 'sys.path' dans le shell Django et reproduisez-le dans votre propre shell Python? –

+0

Le répertoire du projet django et le répertoire immédiat sont tous les deux sur le chemin. Est-ce que chaque répertoire de l'ensemble du projet django doit aussi être sur le chemin? – RyanBrady

+0

@RJBrady: Dans le shell Django, vous pouviez importer depuis 'shells.models'. Donc je pense que '/ home/bradyrj/workspaces/django/shellgame' manquait dans le chemin de votre shell Python. –

Répondre

1

Cela peut fixer dans settings.py:

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

cela a fonctionné sur OSX. – RyanBrady

+0

génial. c'est généralement aussi un problème sur Linux :) – sunn0