如何在上下文处理器 (Django 1.3) 中使用包含 ChoiceField 的 HayStack 表单?

发布于 2024-11-05 00:00:02 字数 864 浏览 0 评论 0原文

我有一个非常简单的 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 技术交流群。

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

发布评论

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

评论(1

維他命╮ 2024-11-12 00:00:02

看看这篇博客文章,它涉及您的情况:

http://blog.e-shell.org /130

但是,这有一个问题。
这样做,选择列表
对于每个字段将生成
启动时间(当您启动
django 开发服务器或 apache 或
lighttpd 或 nginx 或无论你是什么
使用)。这意味着如果您添加一个
维护者组的新用户,它
不会出现在维护者中
字段直到您重新启动服务器!

为了避免这种情况,我们需要添加
使用前当前可用的选择
表单,覆盖默认列表
选择:

您希望在启动表单时设置选择:

class BasicSearchForm(SearchForm):    
    def __init__(self, *args, **kwargs):
        super(BasicSearchForm,self).__init__(*args,**kwargs)
        category_choices = Category.objects.all()
        category_tuples = tuple([(c.id, c.name) for c in category_choices])
        self.fields['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

Take a look at this blog post, it deals with your situation:

http://blog.e-shell.org/130

BUT, there is a problem with that.
Doing it that way, the list of choices
for each field will be generated on
startup time (when you launch the
django development server or apache or
lighttpd or nginx or whatever you are
using). That means that if you add a
new user to the maintainers group, it
will not appear in the maintainer
field until you restart your server!

To avoid that, we will need to add the
current available choices before using
the form, overwriting the default list
of choices:

You want to set the choices whenever the form is initiated:

class BasicSearchForm(SearchForm):    
    def __init__(self, *args, **kwargs):
        super(BasicSearchForm,self).__init__(*args,**kwargs)
        category_choices = Category.objects.all()
        category_tuples = tuple([(c.id, c.name) for c in category_choices])
        self.fields['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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文