Django haystack 自动完成

发布于 2025-01-02 13:07:31 字数 1429 浏览 2 评论 0原文

我正在尝试使用 django-haystack (现在已经大约 2 天了),并且我已经让基本的入门示例正常工作,它看起来非常令人印象深刻。我现在想尝试 haystack 上的自动完成功能。

http://readthedocs.org/docs/django-haystack/en/v1.2.4/ autocomplete.html

第一部分看起来不错:“设置数据”看起来很简单。但是,我不确定“执行查询”需要写在哪里:即我应该包含在哪个视图中:

from haystack.query import SearchQuerySet
sqs = SearchQuerySet().filter(content_auto=request.GET.get('q', ''))

我当前的 urls.py 很简单,设置如下:

urlpatterns = patterns('',
    # Examples:
    ('^hello/$', hello),
    (r'^$', hello), 
    (r'^search/', include('haystack.urls')),
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^mysite/', include('mysite.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    #url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)

查看以下博客:

http://tech.agilitynerd.com/haystack-search-result-ordering-and-pre-rende

我愿意具有类似以下内容:

url(r'^search/', SearchView(load_all=False,searchqueryset=sqs),name='haystack_search'), 

但是,应该在哪里指定 sqs ?在urls.py 或views.py 中?我尝试了两者,但他们给了我一个名称错误“request”,在 sqs 语句中找不到。

I am trying to use django-haystack (been around 2 days now), and I have got the basic Getting Started example working and it looks very impressive. I would now like to try the autocomplete function on haystack.

http://readthedocs.org/docs/django-haystack/en/v1.2.4/autocomplete.html

The first part seems fine: "Setting up the data" seems simple enough. However, I am not sure where the "Performing the Query" needs to be written: i.e in which view should I include:

from haystack.query import SearchQuerySet
sqs = SearchQuerySet().filter(content_auto=request.GET.get('q', ''))

My current urls.py is simple and set up as follows:

urlpatterns = patterns('',
    # Examples:
    ('^hello/

Looking at the following blog:

http://tech.agilitynerd.com/haystack-search-result-ordering-and-pre-rende

I would like to have something like:

url(r'^search/', SearchView(load_all=False,searchqueryset=sqs),name='haystack_search'), 

but, where should the sqs be specified? in the urls.py or views.py? I tried both, but they give me a name error "request" not found on the sqs statement.

, hello), (r'^

Looking at the following blog:

http://tech.agilitynerd.com/haystack-search-result-ordering-and-pre-rende

I would like to have something like:


but, where should the sqs be specified? in the urls.py or views.py? I tried both, but they give me a name error "request" not found on the sqs statement.

, hello), (r'^search/', include('haystack.urls')), # url(r'^

Looking at the following blog:

http://tech.agilitynerd.com/haystack-search-result-ordering-and-pre-rende

I would like to have something like:


but, where should the sqs be specified? in the urls.py or views.py? I tried both, but they give me a name error "request" not found on the sqs statement.

, 'mysite.views.home', name='home'), # url(r'^mysite/', include('mysite.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: #url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: url(r'^admin/', include(admin.site.urls)), )

Looking at the following blog:

http://tech.agilitynerd.com/haystack-search-result-ordering-and-pre-rende

I would like to have something like:

but, where should the sqs be specified? in the urls.py or views.py? I tried both, but they give me a name error "request" not found on the sqs statement.

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

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

发布评论

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

评论(3

贱人配狗天长地久 2025-01-09 13:07:31

通常这会在您自己的 haystack url 中,当前 urls.py 中的 haystack.urls 指向默认 url。创建一个文件 haystack_urls.py 并在 urls.py 中添加它,例如

url(r'^search/', include('yourproject.haystack_urls')),

在该文件中,然后您可以添加自定义代码,例如

from haystack.views import SearchView
from haystack.query import SearchQuerySet

sqs = SearchQuerySet() # edit remove line that was incorret

urlpatterns = patterns('haystack.views',
    url(r'^&', SearchView(load_all=False,searchqueryset=sqs),name='haystack_search'), 
)

包装请求的视图,尝试类似的操作

class SearchWithRequest(SearchView):

    __name__ = 'SearchWithRequest'

    def build_form(self, form_kwargs=None):
        if form_kwargs is None:
            form_kwargs = {}

        if self.searchqueryset is None:
            sqs = SearchQuerySet().filter(content_auto=self.request.GET.get('q', ''))
            form_kwargs['searchqueryset'] = sqs

        return super(SearchWithRequest, self).build_form(form_kwargs)

Usually this would be in your own haystack urls, haystack.urls in your current urls.py is pointing to the default urls. Create a file haystack_urls.py and in your urls.py add it e.g.

url(r'^search/', include('yourproject.haystack_urls')),

in that file you can then add your custom code e.g.

from haystack.views import SearchView
from haystack.query import SearchQuerySet

sqs = SearchQuerySet() # edit remove line that was incorret

urlpatterns = patterns('haystack.views',
    url(r'^&', SearchView(load_all=False,searchqueryset=sqs),name='haystack_search'), 
)

to wrap a view for request try something like

class SearchWithRequest(SearchView):

    __name__ = 'SearchWithRequest'

    def build_form(self, form_kwargs=None):
        if form_kwargs is None:
            form_kwargs = {}

        if self.searchqueryset is None:
            sqs = SearchQuerySet().filter(content_auto=self.request.GET.get('q', ''))
            form_kwargs['searchqueryset'] = sqs

        return super(SearchWithRequest, self).build_form(form_kwargs)
腻橙味 2025-01-09 13:07:31

JamesO 的解决方案对我来说不起作用,所以我定义了一个自定义表单类,它实际上仅覆盖了“搜索”方法。因此,一般来说,您必须尝试

  • 在 urls.py 中使用 FacetedSearchView 代替 basic_searchSearchView
  • 创建您自己的表单类,覆盖搜索方法:

    类 FacetedSearchFormWithAuto(FacetedSearchForm):          
        定义搜索(自我):                        
            如果不是 self.is_valid():
                返回 self.no_query_found()
    
            如果不是 self.cleaned_data.get('q'):
                返回 self.no_query_found()
    
            sqs = self.searchqueryset.autocomplete(content_auto=self.cleaned_data['q'])
    
            如果 self.load_all:
                sqs = sqs.load_all()    
    
            对于 self.selected_facets 中的构面:
                如果“:”不在方面:
                    继续
    
                字段,值=facet.split(“:”,1)
    
                如果值:
                    sqs = sqs.narrow(u'%s:"%s"' % (字段, sqs.query.clean(value)))
    
            返回sqs
    
  • FacetedSearchView 中指定 form_class
    form_class=FacetedSearchFormWithAuto

The solution of JamesO was not working for me, so i defined a custom form class, which is actually overrides the 'search' method only. So, in general, you've got to try

  • At your urls.py use FacetedSearchView insted of basic_search or SearchView.
  • Create your own form class, overriding the search method:

    class FacetedSearchFormWithAuto(FacetedSearchForm):          
        def search(self):                        
            if not self.is_valid():
                return self.no_query_found()
    
            if not self.cleaned_data.get('q'):
                return self.no_query_found()
    
            sqs = self.searchqueryset.autocomplete(content_auto=self.cleaned_data['q'])
    
            if self.load_all:
                sqs = sqs.load_all()    
    
            for facet in self.selected_facets:
                if ":" not in facet:
                    continue
    
                field, value = facet.split(":", 1)
    
                if value:
                    sqs = sqs.narrow(u'%s:"%s"' % (field, sqs.query.clean(value)))
    
            return sqs
    
  • specify form_class in FacetedSearchView
    form_class=FacetedSearchFormWithAuto

女中豪杰 2025-01-09 13:07:31

如果您需要自动完成方面的帮助,我认为这对您有好处,它是完整的解决方案
Django-haystack 全文搜索可以工作,但构面不行

If you need help with autocomplete, I think this would be good for you, it's complete solution
Django-haystack full text search working but facets don't

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