如何使用交互式解释器通过“manage.py shell”重新加载Django模型模块? [英] How do you reload a Django model module using the interactive interpreter via "manage.py shell"?

查看:114
本文介绍了如何使用交互式解释器通过“manage.py shell”重新加载Django模型模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在常规的Python解释器会话中重新加载一个常规的Python模块。这个问题记录了如何做到这一点:



如何卸载(重新加载)Python模块?



由于某些原因,我无法在Django的manage.py shell解释器会话。要重新创建我的问题,请启动在这里找到的基本Django教程:



编写您的第一个Django应用程序,第1部分



创建投票应用程序和投票类后,启动通过manage.py shell解释口译员,并将民意调查应用程序导入其中。

  import polls.models as pm 

创建一个新的投票对象:

  p = pm.Poll()

一切都很好到目前为止还不错。现在回到你的来源并添加任何方法或属性。例如,我添加了:

  def x(self):
return 2 + 2

现在回到解释器并重新加载模块:

  reload(pm)

现在尝试使用你的新方法或属性:

  p1 = pm.Poll()
p1.x()

您会收到以下消息:

 code>'投票'对象没有属性'x'

什么给了?我也尝试重新运行import命令,使用不同的语法导入模块,删除对任何Poll对象或Poll类的所有引用。我也尝试过这两个IPython解释器和纯Python(v2.6)解释器。没有什么似乎可以工作。



在常规解释器会话中使用与任意Python模块相同的技术完美。我似乎无法让它在Django的shell会话中工作。



顺便说一句,如果有任何区别,我在Ubuntu 9.04机器。

解决方案

嗯,我想我必须回答这个。问题是Django将它的模型缓存在一个名为AppCache的单例(像单一的结构中)。基本上,要重新加载Django模型,您需要首先重新加载并重新导入存储在AppCache中的所有模型模块。那么你需要擦掉AppCache。这里是它的代码:

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

curdir = os.getcwd()

在应用程序的cache.get_apps()中:
f = app .__ file__
if f.startswith(curdir)和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

我把所有这一切都放在在Django站点的根目录下,单独的文件叫reloadmodels.py。使用IPython可以通过运行以下方式重新加载所有内容:

 %run〜/ mysite / reloadmodels.py 


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

这篇关于如何使用交互式解释器通过“manage.py shell”重新加载Django模型模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆