在视图中执行的 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 模型的添加是使用管理界面完成的。更改会按预期立即显示在管理界面中。
通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.