Chad Braun-Duin Chad Braun-Duin - 19 days ago 5
Python Question

How do you reload a Django model module using the interactive interpreter via "manage.py shell"?

I know how to reload a regular Python module within a regular Python interpreter session. This question documents how to do that pretty well:

How do I unload (reload) a Python module?

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:

Writing your first Django app, part 1

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.

By the way, if it makes any difference, I'm doing this on a Ubuntu 9.04 machine.

Answer

Well, I think I have to answer to this. The problem is that Django caches its models in a singleton (singleton like structure) called AppCache. Basically, to reload Django models you need to first reload and re-import all the model modules stored in the AppCache. Then you need to wipe out the AppCache. Here's the code for it:

import os
from django.db.models.loading import AppCache
cache = AppCache()

curdir = os.getcwd()

for app in cache.get_apps():
    f = app.__file__
    if f.startswith(curdir) and f.endswith('.pyc'):
        os.remove(f)
    __import__(app.__name__)
    reload(app)

from django.utils.datastructures import SortedDict
cache.app_store = SortedDict()
cache.app_models = SortedDict()
cache.app_errors = {}
cache.handled = {}
cache.loaded = False

I've put all of this in a separate file called reloadmodels.py in the root directory of my Django site. Using IPython I can reload everything by running:

%run ~/mysite/reloadmodels.py
Comments