当前位置: 首页>>代码示例>>Python>>正文


Python EmptySearchQuerySet.order_by方法代码示例

本文整理汇总了Python中haystack.query.EmptySearchQuerySet.order_by方法的典型用法代码示例。如果您正苦于以下问题:Python EmptySearchQuerySet.order_by方法的具体用法?Python EmptySearchQuerySet.order_by怎么用?Python EmptySearchQuerySet.order_by使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在haystack.query.EmptySearchQuerySet的用法示例。


在下文中一共展示了EmptySearchQuerySet.order_by方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: basic_search

# 需要导入模块: from haystack.query import EmptySearchQuerySet [as 别名]
# 或者: from haystack.query.EmptySearchQuerySet import order_by [as 别名]
def basic_search(request, template='search/search.html', load_all=True, form_class=ModelSearchForm, searchqueryset=None, context_class=RequestContext, extra_context=None, results_per_page=None, sort_by=[]):
    """
    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()

    try:
        del request.session['selected_facets']
    except:
        pass
    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 = SearchQuerySet().all()

    selected_facets = request.GET.getlist('selected_facets')
    selected_date_facets = request.GET.getlist('selected_date_facets')
    selected_facets = FacetSet(facet_string_list=selected_facets,
            date_facet_string_list=selected_date_facets)

    request.session['selected_facets'] = selected_facets
    results = selected_facets.narrow(results)

    if not sort_by == ['tmp']:
        results = results.order_by(*sort_by)
    results = results.order_by('-score')
    if not template:
        return results
    count = results.count()
    paginator = Paginator(results, results_per_page or 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,
        'count': count,
    }

    if getattr(settings, 'HAYSTACK_INCLUDE_SPELLING', False):
        context['suggestion'] = form.get_suggestion()

    if extra_context:
        context.update(extra_context)

    return render_to_response(template, context, context_instance=context_class(request))
开发者ID:shaunokeefe,项目名称:gigs,代码行数:77,代码来源:views.py

示例2: catalogue_search

# 需要导入模块: from haystack.query import EmptySearchQuerySet [as 别名]
# 或者: from haystack.query.EmptySearchQuerySet import order_by [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()
#.........这里部分代码省略.........
开发者ID:uq-eresearch,项目名称:uqam,代码行数:103,代码来源:views.py


注:本文中的haystack.query.EmptySearchQuerySet.order_by方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。