本文整理汇总了Python中haystack.query.EmptySearchQuerySet.narrow方法的典型用法代码示例。如果您正苦于以下问题:Python EmptySearchQuerySet.narrow方法的具体用法?Python EmptySearchQuerySet.narrow怎么用?Python EmptySearchQuerySet.narrow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类haystack.query.EmptySearchQuerySet
的用法示例。
在下文中一共展示了EmptySearchQuerySet.narrow方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: search
# 需要导入模块: from haystack.query import EmptySearchQuerySet [as 别名]
# 或者: from haystack.query.EmptySearchQuerySet import narrow [as 别名]
def search(request, template='search/search.html', load_all=True,
form_class=ModelSearchForm, searchqueryset=None,
extra_context=None,
results_per_page=None):
"""
"""
context = RequestContext(request)
query = ''
results = EmptySearchQuerySet()
if request.GET.get('q'):
form = form_class(request.GET, searchqueryset=searchqueryset, load_all=load_all)
if form.is_valid():
query = form.cleaned_data['q']
results = form.search()
context.update({'last_search': query})
# if return no result, try it again with autocomplete search word
if results.count() == 0:
results = SearchQuerySet().autocomplete(enfname=request.GET.get('q', ''))
else:
form = form_class(searchqueryset=searchqueryset, load_all=load_all)
selected_facets_list = []
if request.GET.getlist('selected_facets'):
for facet in request.GET.getlist('selected_facets'):
if ":" not in facet:
continue
field, value = facet.split(":", 1)
results = results.narrow(u'%s:"%s"' % (field, value))
# 生成页面的facet
selected_facets_list.append(('&selected_facets='+facet, value))
selected_facets_context = list({
'name': i[1],
'rm_selected_facet': ''.join(list(j[0] for j in selected_facets_list if j!=i))}
for i in selected_facets_list)
context.update({'selected_facets_context': selected_facets_context})
def facet_field_to_selector(sqs, name, context_name):
try:
fields = sqs.facet(name).facet_counts()['fields'][name]
return {'name': context_name,
'exact_name': name,
'options': list({'context': i} for i in fields)}
except:
pass
return None
selectors = []
# 找出品牌的facet
selectors.append(facet_field_to_selector(results, 'brand', '品牌'))
# 找出品类的facet
selectors.append(facet_field_to_selector(results, 'categ', '品类'))
context.update({'selectors': selectors})
rm_invalid_result = []
for r in results:
try:
tmp = r._get_object()
if tmp:
rm_invalid_result.append(tmp)
except:
pass
paginator = HXPaginator(rm_invalid_result, results_per_page or 40)
page = int(request.GET.get('page', 1))
try:
templates = paginator.page(page)
except PageNotAnInteger:
page = 1
except EmptyPage:
page = paginator.num_pages
context.update({
'form': form,
'templates': templates,
'paginator': paginator,
'query': query,
'suggestion': None,
})
if results.query.backend.include_spelling:
context['suggestion'] = form.get_suggestion()
if extra_context:
context.update(extra_context)
return TemplateResponse(request, template, context)
示例2: catalogue_search
# 需要导入模块: from haystack.query import EmptySearchQuerySet [as 别名]
# 或者: from haystack.query.EmptySearchQuerySet import narrow [as 别名]
def catalogue_search(request, template='search/search.html', load_all=True,
form_class=CatalogueSearchForm, searchqueryset=None, extra_context=None,
results_per_page=None):
"""
A more traditional view that also demonstrate an alternative
way to use Haystack.
Useful as an example of for basing heavily custom views off of.
Also has the benefit of thread-safety, which the ``SearchView`` class may
not be.
Template:: ``search/search.html``
Context::
* form
An instance of the ``form_class``. (default: ``ModelSearchForm``)
* page
The current page of search results.
* paginator
A paginator instance for the results.
* query
The query received by the form.
"""
query = ''
results = EmptySearchQuerySet()
results_per_page = results_per_page or RESULTS_PER_PAGE
if request.GET.get('q'):
form = form_class(request.GET, searchqueryset=searchqueryset, load_all=load_all)
if form.is_valid():
query = form.cleaned_data['q']
results = form.search()
else:
form = form_class(searchqueryset=searchqueryset, load_all=load_all)
results = results.facet('categories').facet('country').facet('has_images').facet('global_region').facet('item_name')
if form.is_valid():
results = filter_with_facet(form, results, 'item_name', 'item_name')
results = filter_with_facet(form, results, 'category', 'categories')
results = filter_with_facet(form, results, 'global_region', 'global_region')
results = filter_with_facet(form, results, 'country', 'country')
if form.cleaned_data['person']:
sqs = SearchQuerySet()
for p in form.cleaned_data['person'].split():
term = sqs.query.clean(p)
results = results.narrow(u'people:"%s"' % term)
if form.cleaned_data['has_images'] == True:
results = results.narrow(u'has_images_exact:true')
facets = results.facet_counts()
if facets:
# Prepare the form with all the available facets
load_facets_into_form(form, facets, 'item_name', 'item_name')
load_facets_into_form(form, facets, 'category', 'categories')
load_facets_into_form(form, facets, 'global_region', 'global_region')
load_facets_into_form(form, facets, 'country', 'country')
# Append count of images into form
appended_count = False
for name, val in facets['fields']['has_images']:
if name == 'true':
form.fields['has_images'].label += ' (%s)' % val
appended_count = True
if not appended_count:
form.fields['has_images'].label += ' (0)'
results = results.order_by('registration_number')
paginator = Paginator(results, results_per_page)
try:
page = paginator.page(int(request.GET.get('page', 1)))
except InvalidPage:
raise Http404("No such page of results!")
context = {
'form': form,
'page': page,
'paginator': paginator,
'query': query,
'suggestion': None,
'facets': facets,
}
# Store query for paging through results on item detail page
if form.is_valid():
request.session['search_query'] = form.cleaned_data
# Access the first result to prevent ZeroDivisionError
if results:
results[0]
request.session['search_results'] = results
request.session['search_results_per_page'] = results_per_page
if getattr(settings, 'HAYSTACK_INCLUDE_SPELLING', False):
context['suggestion'] = form.get_suggestion()
#.........这里部分代码省略.........