我是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,
})
任何提示将不胜感激。我在这里只有几天,所以如果您有解决方案,请记住这一点。