2011-05-10 96 views
3

我有查询模型来填充表单视图:Django的查询返回旧数据

class AddServerForm(forms.Form): 
    …snip… 
    # Compile and present choices for HARDWARE CONFIG 
    hwChoices = HardwareConfig.objects.\ 
     values_list('name','description').order_by('name') 
    hwChoices = [('', '----- SELECT ONE -----')] +\ 
     [ (x,'{0} - {1}'.format(x,y)) for x,y in hwChoices ] 

    hwconfig = forms.ChoiceField(label='Hardware Config', choices=hwChoices) 
    …snip… 

def addServers(request, template="manager/add_servers.html", 
    template_success="manager/add_servers_success.html"): 
    if request.method == 'POST': 
     # …snip… - process the form 
    else: 
     # Page was called via GET - use a default form 
     addForm = AddServerForm() 

    return render_to_response(template, dict(addForm=addForm), 
     context_instance=RequestContext(request)) 

增加的HardwareConfig模型所使用的管理界面来完成。按预期在管理界面中立即显示更改。

运行通过外壳的查询将返回所有结果预期:

[email protected]> python manage.py shell 
Python 2.6 (r26:66714, Feb 21 2009, 02:16:04) 
[GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
(InteractiveConsole) 
>>> from serverbuild.base.models import HardwareConfig 
>>> hwChoices = HardwareConfig.objects.\ 
...   values_list('name','description').order_by('name') 

hwChoices现在包含完整的结果集。

但是,加载addServers视图(上面)返回旧的结果集,缺少新添加的条目。

我必须重新启动网络服务器才能显示所做的更改,看起来好像该查询正在某处被缓存。

  • 我没有做任何显式缓存的任何地方(grep -ri cache /project/root返回任何)
  • 这不是浏览器缓存的页面 - 通过镀铬工具检查,使用不同的用户&计算机还试图

怎么回事,我该如何解决?


版本:

  • MySQLdb的:1.2.2
  • django的:1.2.5
  • 蟒:2.6

回答

3

hwChoices定义的形式时被评估 - 即当过程开始时。

请改为使用表格的__init__方法进行计算。

+0

啊!我现在看到它......谢谢。在我的情况下,我已经将整个主体移动到了__init__中......我从性能角度来看最好尽可能少地在__init__中执行,但是对于我的代码来说,这是过早的优化。除了性能之外,除了将计算转移到'__init__'之外,还有其他原因吗? – MikeyB 2011-05-10 21:12:19