您好,谢谢您的帮助!
在wagtail 2.2.2项目中,我最近在PostgreSQL搜索后端添加了搜索功能。直到我尝试通过给定的标记在经过过滤的PageQuerySet
上运行搜索,这都会引发此错误:
FilterFieldError at /blog/tags/tag_slug/
Cannot filter search results with field "tag_id". Please add index.FilterField('tag_id') to BlogPost.search_fields.
Django Version: 2.0.13
Python Version: 3.6.8
...
File "/path/to/env/lib/python3.6/site-packages/wagtail/search/queryset.py" in search
12. operator=operator,order_by_relevance=order_by_relevance,partial_match=partial_match)
File "/path/to/env/lib/python3.6/site-packages/wagtail/search/backends/base.py" in search
371. partial_match=partial_match,File "/path/to/env/lib/python3.6/site-packages/wagtail/search/backends/base.py" in _search
359. search_query.check()
File "/path/to/env/lib/python3.6/site-packages/wagtail/search/backends/base.py" in check
157. self._get_filters_from_where_node(self.queryset.query.where,check_only=True)
File "/path/to/env/lib/python3.6/site-packages/wagtail/search/backends/base.py" in _get_filters_from_where_node
108. child_filters = [self._get_filters_from_where_node(child) for child in where_node.children]
File "/path/to/env/lib/python3.6/site-packages/wagtail/search/backends/base.py" in <listcomp>
108. child_filters = [self._get_filters_from_where_node(child) for child in where_node.children]
File "/path/to/env/lib/python3.6/site-packages/wagtail/search/backends/base.py" in _get_filters_from_where_node
100. return self._process_filter(field_attname,lookup,value,check_only=check_only)
File "/path/to/env/lib/python3.6/site-packages/wagtail/search/backends/base.py" in _process_filter
73. field_name=field_attname
Exception Type: FilterFieldError at /trust-worthy/tags/presentation/
Exception Value: Cannot filter search results with field "tag_id". Please add index.FilterField('tag_id') to BlogPost.search_fields.
我将FilterField
添加到BlogPost模型的search_fields中,就像说的那样,并且在过滤后的集合上返回正确的搜索结果时,它似乎可以很好地工作。现在唯一的问题是,每次Django启动时,它都会显示以下警告:
System check identified some issues:
WARNINGS:
blog.BlogPost: BlogPost.search_fields contains non-existent field 'tag_id'
以下是有关模型设置的基本摘要:
from wagtail.core.models import Page
from wagtail.search import index
from taggit.models import Tag,TaggedItemBase
from modelcluster.contrib.taggit import ClusterTaggableManager
from modelcluster.fields import ParentalKey
class BlogIndex(Page):
get_context(self,request):
context = super().get_context(request)
tag = Tag.objects.get(slug=some_tag_slug)
pages = BlogPost.objects.descendant_of(self)\
.live().public().order_by('-date')
# The above <PageQuerySet> works fine to search
# until I add this valid working filter:
pages = pages.filter(tags=tag)
# ...which causes the original error when searching that is
# fixed by adding `tag_id` FilterField to BlogPost.search_fields
pages = pages.search(some_search_query)
# `pages` is now valid <PostgresSearchResults>
context['pages'] = pages
return context
class BlogPostTag(TaggedItemBase):
content_object = ParentalKey('blog.BlogPost',related_name='tagged_items')
class BlogPost(Page):
search_fields = Page.search_fields + [
index.FilterField('tag_id'),# This fixes the error but causes the warning
]
tags = ClusterTaggableManager(through=BlogPostTag,blank=True)
我的问题是-我在做错什么导致引起关于不存在字段的警告?现在,在筛选后的列表上搜索可以正常工作,但是tag_id
不在BlogPost模型上的字段上也确实如此。是否有另一种正确的方法可以解决Cannot filter search results
错误并获得相同的结果?