Cannot reload a Django models module within a "manage.py shell" session

419 views
Skip to first unread message

chadbraunduin

unread,
May 21, 2009, 9:54:54 PM5/21/09
to Django users
I know how to reload a regular Python module within a regular Python
interpreter session. For some reason, I am having trouble doing that
within Django's "manage.py shell" interpreter session. To recreate my
issue, start the basic Django tutorial found here:

http://docs.djangoproject.com/en/dev/intro/tutorial01/#intro-tutorial01

After creating the "polls" application and "Poll" class, start up the
interpreter via "manage.py shell" and import the "polls" app into it.

import polls.models as pm

Create a new "Poll" object:

p = pm.Poll()

All is well and good so far. Now go back to your source and add any
arbitrary method or attribute. For example, I've added:

def x(self):
return 2+2

Now go back to the interpreter and "reload" the module:

reload(pm)

Now try to use your new method or attribute:

p1 = pm.Poll()
p1.x()

You'll get this message:

'Poll' object has no attribute 'x'

What gives? I've also tried rerunning the import command, importing
the module using different syntax, deleting all references to any
"Poll" objects or to the "Poll" class. I've also tried this with both
the IPython interpreter and with the plain Python (v2.6) interpreter.
Nothing seems to work.

Using the same techniques with an arbitrary Python module in a regular
interpreter session works perfectly. I just can't seem to get it to
work in Django's "shell" session.

Sam Chuparkoff

unread,
May 22, 2009, 12:49:51 AM5/22/09
to django...@googlegroups.com
Reloading your models module is not going to give you different model
classes, because django tracks these classes by name. This is why the
tutorial instructs you to start a new shell after you've modified your
models.

sdc


Reply all
Reply to author
Forward
0 new messages