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

前端 未结 9 1085
广开言路
广开言路 2020-11-28 02:14

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

9条回答
  •  悲&欢浪女
    2020-11-28 03:09

    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:

    1. Make changes to the model (MyModel)
    2. remove models.pyc
    3. 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
      
    4. Reload model like here

      reload(project.app.models)
      from project.app.models import MyModel
      

提交回复
热议问题