如何在上下文处理器 (Django 1.3) 中使用包含 ChoiceField 的 HayStack 表单?
我有一个非常简单的 Haystack 表单,看起来像这样:
class BasicSearchForm(SearchForm):
category_choices = Category.objects.all()
category_tuples = tuple([(c.id, c.name) for c in category_choices])
category = forms.ChoiceField(choices=category_tuples, required=False)
def search(self):
sqs = super(BasicSearchForm, self).search()
if self.cleaned_data['category']:
if self.cleaned_data['category'] != "*":
sqs = sqs.filter(category__id=self.cleaned_data['category'])
return sqs
然后我有一个像这样的上下文处理器:
def search_form(request):
basic_search = BasicSearchForm()
return { 'basic_search': basic_search }
出于某种原因,创建一个新的类别对象(通过 Django 管理并保存它)将不会更新我使用的类别元组直到形式的 ChoiceField 我重新启动 Apache。
有谁知道这可能是什么原因造成的?
提前致谢!
I have a pretty simple Haystack form that looks just like this:
class BasicSearchForm(SearchForm):
category_choices = Category.objects.all()
category_tuples = tuple([(c.id, c.name) for c in category_choices])
category = forms.ChoiceField(choices=category_tuples, required=False)
def search(self):
sqs = super(BasicSearchForm, self).search()
if self.cleaned_data['category']:
if self.cleaned_data['category'] != "*":
sqs = sqs.filter(category__id=self.cleaned_data['category'])
return sqs
I then have a context processor just like this:
def search_form(request):
basic_search = BasicSearchForm()
return { 'basic_search': basic_search }
For some reason, creating a new category object (via the Django admin, and saving it) will not update my category tuple used in the ChoiceField of the form until I restart Apache.
Does anyone know what could be causing this?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这篇博客文章,它涉及您的情况:
http://blog.e-shell.org /130
您希望在启动表单时设置选择:
Take a look at this blog post, it deals with your situation:
http://blog.e-shell.org/130
You want to set the choices whenever the form is initiated: