我有一个 django 项目,需要搜索 2 个不同的模型,其中一个模型有 3 种类型,我需要根据它们进行过滤。我已经安装了 haystack,并且在基本意义上工作(使用我的模型的默认 url conf 和 SearchView 以及入门文档中的模板返回的结果很好)。
问题是我只能通过使用基本 search.html 模板中的搜索表单来获取结果,并且我正在尝试使全局搜索栏与 haystack 一起使用,但我似乎无法得到正确的结果,我我对干草堆文档的运气不太好。我在这里发现了另一个问题,导致我在搜索应用程序中使用了以下方法。
我的 urls.py 将“/search”定向到我的 search.views 中的此视图:
def search_posts(request):
post_type = str(request.GET.get('type')).lower()
sqs = SearchQuerySet().all().filter(type=post_type)
view = search_view_factory(
view_class=SearchView,
template='search/search.html',
searchqueryset=sqs,
form_class=HighlightedSearchForm
)
return view(request)
传入的 url 字符串类似于:
这将从我的全局搜索栏中获取查询字符串,但不返回任何结果,但是如果我从 sqs 行中删除 .filter(type=post_type) 部分,我将再次获得搜索结果(尽管未按帖子类型过滤)。有任何想法吗?我认为我错过了一些相当明显的东西,但我似乎无法弄清楚这一点。
谢谢,
-肖恩
EDIT:
原来我只是个白痴。我按类型对 SQS 进行过滤未返回结果的原因是因为我的 PostIndex 类中没有包含类型字段。我将 PostIndex 更改为:
class PostIndex(indexes.SearchIndex, indexes.Indexable):
...
type = indexes.CharField(model_attr='type')
并重建,现在一切正常。
不过还是谢谢你的回复!
def search_posts(request):
post_type = str(request.GET.get('type')).lower()
sqs = SearchQuerySet().filter(type=post_type)
clean_query = sqs.query.clean(post_type)
result = sqs.filter(content=clean_query)
view = search_view_factory(
view_class=SearchView,
template='search/search.html',
searchqueryset=result,
form_class=HighlightedSearchForm
)
return view(request)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)