1

您好,提前感谢您的帮助!

在 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错误并获得相同的结果?

4

0 回答 0