GJ. GJ. - 1 year ago 125
Python Question

How to reload Django models without losing my locals in an interactive session?

I'm doing some research with an interactive shell and using a Django app (shell_plus) for storing data and browsing it using the convenient admin.

Occasionally I add or change some of the app models, and run a syncdb (or South migration when changing a model). The changes to the models don't take effect in my interactive session even if I re-import the app models. Thus I'm forced to restart the shell_plus and lose my precious locals() in the process.

Is there any way to reload the models during a session?

Answer Source

You can use this snippet to rebuild the AppCache. Do not forget to remove all *.pyc files if any by using something like:

find . -name "*.pyc" -exec rm {} \;

Otherwise the reload() will ignore your changes in your models.py file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download