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

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

问题描述

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

I know how to reload a regular Python module within a regular Python interpreter session. This question documents how to do that pretty well:

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

出于某种原因,我在 Django 的manage.py shell"解释器会话中无法做到这一点.要重新创建我的问题,请开始在此处找到的基本 Django 教程:

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:

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

创建polls"应用程序和Poll"类后,通过manage.py shell"启动解释器并将polls"应用程序导入其中.

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

创建一个新的Poll"对象:

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()

您会收到此消息:

'Poll' object has no attribute 'x'

什么给?我还尝试重新运行导入命令,使用不同的语法导入模块,删除对任何Poll"对象或Poll"类的所有引用.我也用 IPython 解释器和普通的 Python (v2.6) 解释器尝试过这个.似乎没有任何效果.

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.

在常规解释器会话中对任意 Python 模块使用相同的技术非常有效.我似乎无法让它在 Django 的shell"会话中工作.

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.

顺便说一句,如果有什么不同,我是在 Ubuntu 9.04 机器上进行的.

By the way, if it makes any difference, I'm doing this on a Ubuntu 9.04 machine.

推荐答案

好吧,我想我必须回答这个问题.问题在于 Django 将其模型缓存在一个名为 AppCache 的单例(类似单例的结构)中.基本上,要重新加载 Django 模型,您需要首先重新加载并重新导入存储在 AppCache 中的所有模型模块.然后你需要清除 AppCache.这是它的代码:

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

我已将所有这些放在我的 Django 站点根目录下一个名为 reloadmodels.py 的单独文件中.使用 IPython 我可以通过运行重新加载所有内容:

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天全站免登陆