1

我是 Wagtail/Django 的新手。开始很容易,我的应用程序很容易启动并运行。但是,我在搜索时遇到了问题。该应用程序很小,我想使用后端数据库(或 MSSQL)。我到处搜索,但令人惊讶的是没有找到解决方案。希望能得到一个她:-)

我有一个带有内联元素的页面。简化如下

class BlockPage(Page):
    content = RichTextField()

    content_panels = Page.content_panels + [
        FieldPanel('content'),
        InlinePanel('rel_page_blocks', label="Markup blocks"),
    ]

    search_fields = [
        index.SearchField('content', partial_match=True),  
        index.RelatedFields('rel_page_blocks', [
            index.SearchField('block_content', partial_match=True),
        ]),
        index.FilterField('live'),
    ]

内联面板是这样定义的

class Blocks(index.Indexed, Orderable):
    page = ParentalKey(BlockPage, related_name='rel_page_blocks')
    block_content = RichTextField()

    panels = [
        FieldPanel('block_content'),
   ]

到目前为止,一切都很好。现在我想用它的内联块搜索页面。我已成功搜索该页面,但未搜索。我知道这个问题可以使用弹性搜索解决,但它不是我的选择。

她是views.py

def search(request):
    search_query = request.GET.get('query', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        blocks_results = BlockPage.objects.live().search(search_query)     
        blocks_result_ids = [p.page_ptr.id for p in blocks_results]

# I get a result using this but I would like it to be attached to the 
# parent so when viewing the result I can refere/show the parent BlockPage
# The question is how to attach "r" to the parent result?  
 r = get_search_backend().search(search_query, Blocks)

    page_ids = blocks_result_ids + **other page ids**
    search_results = Page.objects.filter(id__in=page_ids)        

    query = Query.get(search_query)
    # Record hit
    query.add_hit()
else:
    search_results = Page.objects.none()

return render(request, 'search/search.html', {
    'search_query': search_query,
    'search_results': search_results,
})

任何提示将不胜感激。我在这里只有几天大,所以如果您有解决方案,请记住这一点。

4

0 回答 0