39
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
7
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
See results:
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:
- Django 1.10
- Python 3.4
- django_extension 1.7.4
- Based (primary) on https://django-extensions.readthedocs.io/en/latest/shell_plus.html and http://ipython.readthedocs.io/en/stable/config/extensions/autoreload.html
- Caution. It is may be produce an error, if you try change a code where used super().
- [Django]-Django testing: Test the initial value of a form field
- [Django]-What is the purpose of NGINX and Gunicorn running in parallel?
- [Django]-Sending an SMS to a Cellphone using Django
6
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.
- [Django]-Stack trace from manage.py runserver not appearing
- [Django]-How to get the username of the logged-in user in Django?
- [Django]-Annotating a Sum results in None rather than zero
5
Assuming your project is set up this way
- project name : bookstore
- app name : shelf
- model name : Books
first load
from bookstore.shelf.models import Books
subsequent reloads
import bookstore;reload(bookstore.shelf.models);from bookstore.shelf.models import Books
- [Django]-Django Unique Together (with foreign keys)
- [Django]-Change a Django form field to a hidden field
- [Django]-A Better Django Admin ManyToMany Field Widget
4
As far as I’m concerned, none of the above solutions worked on their own, also this thread didn’t help much on its own, but after combining the approaches I managed to reload my models in shell_plus:
- Make changes to the model (MyModel)
- remove
models.pyc
-
Clean Django model cache (like here):
from django.db.models.loading import AppCache cache = AppCache() from django.utils.datastructures import SortedDict cache.app_store = SortedDict() cache.app_models = SortedDict() cache.app_errors = {} cache.handled = {} cache.loaded = False
-
Reload model like here
reload(project.app.models) from project.app.models import MyModel
- [Django]-Example of Django Class-Based DeleteView
- [Django]-Error: No module named staticfiles
- [Django]-Django returns 403 error when sending a POST request
3
ipython console does a deep reload with each reload()
expression; and of course adds a lot of other useful stuff.
- [Django]-How do I display the value of a Django form field in a template?
- [Django]-Django: TemplateDoesNotExist (rest_framework/api.html)
- [Django]-How do I package a python application to make it pip-installable?
3
From the answers of Seti Volkylany and pv
- Install IPython:
pip install ipython
- Run
python manage.py shell
: the symbol at the beginning of a line should now beIn [1]:
(in cmd it was>>>
) - Run
ipython profile create
-
Go in
~/.ipython/profile_default/ipython_config.py
and open it in a text editor and add these two lines at the end:c.InteractiveShellApp.extensions = [‘autoreload’]
c.InteractiveShellApp.exec_lines = [‘%autoreload 2’]
You can now run python manage.py shell
, edit your models without having to write %autoreload 2
- [Django]-How to change a django QueryDict to Python Dict?
- [Django]-How to receive json data using HTTP POST request in Django 1.6?
- [Django]-Django – Highlight Navigation based on current page?
1
Enable IPython autoreload extension before importing any code:
%load_ext autoreload
%autoreload 2
I use it with the regular django shell and it works perfectly, although it does have some limitations:
*Caveats:
Reloading Python modules in a reliable way is in general difficult, and unexpected things may occur. %autoreload tries to work around common pitfalls by replacing function code objects and parts of classes previously in the module with new versions. This makes the following things to work:
- Functions and classes imported via ‘from xxx import foo’ are upgraded to new versions when ‘xxx’ is reloaded.
- Methods and properties of classes are upgraded on reload, so that calling ‘c.foo()’ on an object ‘c’ created before the reload causes the new code for ‘foo’ to be executed.
Some of the known remaining caveats are:
- Replacing code objects does not always succeed: changing a @property in a class to an ordinary method or a method to a member variable can cause problems (but in old objects only).
- Functions that are removed (eg. via monkey-patching) from a module before it is reloaded are not upgraded.
- C extension modules cannot be reloaded, and so cannot be autoreloaded.*
source: https://ipython.org/ipython-doc/3/config/extensions/autoreload.html#caveats
Another great option is to write your code in a separate script and send it to django shell, like this:
manage.py shell < my_script.py
- [Django]-Django: Assigning variables in template
- [Django]-Django comparing model instances for equality
- [Django]-Allowing only super user login
0
I wasn’t able to get any of the above solutions to work, but I did come up with a workaround for reloading any other non-models module in my django project (e.g. a functions.py
or views.py
module).
-
Create a file called
reimport_module.py
. I stored it in thelocal/
folder of my django project on my dev machine.# Desc: Imports the module with the name passed in, or imports it for first # time if it hasn't already been imported. # # Purpose of this script is to speed up development of functions that # are written in an external editor then tested in IPython. # # Without this script you have to exit & reenter IPython then redo # import statements, definitions of local variables, etc. # # Note: doesn't work for Django models files, because Django caches # them in a structure called AppCache. # # Args: module to reload (string) import sys module_to_reload = sys.argv[1] # Attempt to pop module try: sys.modules.pop(module_to_reload) print 'reimporting...' except KeyError: print 'importing for first time...' # (re)import module import_str = 'from {0} import *'.format(module_to_reload) exec(import_str)
-
Launch shell plus (which uses an embedded IPython shell):
python manage.py shell_plus
-
Use the following to import the module you are developing:
%run local/reimport_module.py 'your.module'
-
Use IPython to test functions in your module.
- Make changes to the module in an external editor.
-
Use the following to reimport the module without having to exit & reenter IPython:
%run local/reimport_module.py 'your.module'
Note: this command was already used in step 3, so you can type
%run
then the up arrow to autocomplete it.
- [Django]-Celery. Decrease number of processes
- [Django]-In Django – Model Inheritance – Does it allow you to override a parent model's attribute?
- [Django]-Django REST framework serializer without a model