0

我已经为文章和用户创建了两个索引,我希望创建一个索引,这样当搜索来宾时,搜索结果将显示在页面顶部与搜索条件匹配的用户和匹配它的底部文章中。

到目前为止,我能够创建两个不同的索引并构建它们并获得搜索结果以显示它们。但是,问题是,我想在两个不同的对象中展示它们。我怎样才能做到这一点?

索引

class UserIndex(indexes.SearchIndex):
    text = indexes.CharField(document=True, use_template=True)
    pub_date = indexes.DateTimeField(model_attr='date_joined')
    full_name = indexes.CharField(model_attr='full_name', )
    bio = indexes.CharField(model_attr='bio', null=True)

    def index_queryset(self):
        return User.objects.filter(is_active=True, email_is_valid=True)


site.register(User, UserIndex)


class ArticleIndex(indexes.SearchIndex):
    text = indexes.CharField(document=True, use_template=True)
    pub_date = indexes.DateTimeField(model_attr='created_at')


    def index_queryset(self):
        return Article.global_objects.get_active()


site.register(Article, ArticleIndex)

在我看来

sqs = SearchQuerySet().filter(site_id=settings.SITE_ID, status=Article.STATUS_ACTIVE)


def query(request, load_all=True, form_class=ModelSearchForm, searchqueryset=None, context_class=RequestContext,
          extra_context=None, results_per_page=None):
    """

    """

    query = ''
    filter = {}
    context = {}
    results = EmptySearchQuerySet()
    active_tab = 'most-recent'

    ordering_tabs = {
        'most-discussed': _('Most discussed'),
        'most-viewed': _('Most viewed'),
        'highest-rank': _('Highest rank'),
        'most-recent': _('Most recent'),
    }
    tab = request.GET.get('tab', 'most-recent')

    # if searching inside a category
    category_id = request.GET.get('category', None)
    if category_id:
        category = Category.objects.get(pk=category_id)
        context.update({
            'category_id': category_id,
            'category': category
        })
        filter.update({
            'category_id': category_id
        })

    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']

            if tab == 'most-discussed':
                results = form.search().filter(**filter).order_by('-comments')
            elif tab == 'highest-rank':
                results = form.search().filter(**filter).order_by('-rank')
            elif tab == 'most-viewed':
                results = form.search().filter(**filter).order_by('-views')
            else:
                results = form.search().filter(**filter).order_by('-pub_date')

            # remove active tab from ordering_tabs dictionary
            active_tab = ordering_tabs.get(tab)
            ordering_tabs.pop(tab, None)



    else:
        form = form_class(searchqueryset=searchqueryset, load_all=load_all)

    paginator = Paginator(results, results_per_page or 8, adjacent_pages=3)

    try:
        page = paginator.page(int(request.GET.get('page', 1)))
    except InvalidPage:
        raise Http404(_('No such page of results!'))

    context.update({
        'form': form,
        'page': page,
        'paginator': paginator,
        'query': query,
        'active_tab': active_tab,
        'tab': tab,
        'ordering_tabs': ordering_tabs,
    })

    if extra_context:
        context.update(extra_context)

    return render_to_response('search/search.html', context, RequestContext(request))
4

0 回答 0