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.
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
My solution on 2016 (in future it may be changed)
1.Install django_extension
2.Add next settings:
SHELL_PLUS = 'ipython'
IPYTHON_ARGUMENTS = [
'--ext', 'autoreload',
]
3.Run shell
./manage.py shell_plus
model example
class Notification(models.Model):
........
@classmethod
def get_something(self):
return 'I am programmer'
In shell
In [1]: Notification.get_something()
Out[1]: 'I am programmer'
Made changes on model
@classmethod
def get_something(self):
return 'I am Python programmer'
In shell
# shell does not display changes
In [2]: Notification.get_something()
Out[2]: 'I am programmer'
In shell. This is a magic
# configure extension of ipython
In [3]: %autoreload 2
In shell
# try again - all worked
In [4]: Notification.get_something()
Out[4]: 'I am Python programmer'
Made changes again
@classmethod
def get_something(self):
return 'I am full-stack Python programmer'
In shell
# all worked again
In [5]: Notification.get_something()
Out[5]: 'I am full-stack Python programmer'
Drawback: 1. Need manually run code
%autoreload 2
since django_extension 1.7 has not support for run arbitrary code. May be in future release it has this feature.
Notes:
You can also use django-extensions project with the following command:
manage.py shell_plus --notebook
This will open a IPython notebook on your web browser instead of the IPython shell interpreter. Write your code there, and run it.
When you change your modules, just click on the web page menu item 'Kernel->Restart'
Re-running the code now uses your modified modules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With