在视图中执行的 Django 查询返回旧数据

发布于 2024-11-06 20:12:57 字数 1776 浏览 3 评论 0原文

我有一个查询模型以填充表单的视图:

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 模型的添加是使用管理界面完成的。更改会按预期立即显示在管理界面中。

通过 shell 运行查询会按预期返回所有结果:

michael@victory> 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 不返回任何内容)
  • 这不是浏览器缓存页面 - 通过 chrome 工具检查,也尝试使用不同的用户&计算机

出了什么问题以及如何修复它?


版本:

  • MySQLdb:1.2.2
  • django:1.2.5
  • python:2.6

I have a view which queries a model to populate a form:

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

Additions to the HardwareConfig model are done using the admin interface. Changes show up immediately in the admin interface as expected.

Running the query via the shell returns all results as expected:

michael@victory> 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 now contains the complete set of results.

However, loading the addServers view (above) returns the old result set, missing the newly-added entries.

I have to restart the webserver in order for the changes to show up which makes it seem as though that query is being cached somewhere.

  • I'm not doing any explicit caching anywhere (grep -ri cache /project/root returns nothing)
  • It's not the browser caching the page - inspected via chrome tools, also tried using a different user & computer

What's going wrong and how do I fix it?


Versions:

  • MySQLdb: 1.2.2
  • django: 1.2.5
  • python: 2.6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毅然前行 2024-11-13 20:12:57

hwChoices 在定义表单时(即进程启动时)进行评估。

请改为在表单的 __init__ 方法中进行计算。

hwChoices is evaluated when the form is defined - ie when the process starts.

Do the calculation in the form's __init__ method instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文