0

我在 wagtail django 中搜索模型时遇到了一些危机

这是我的模型对象代码

recipes = RecipePage.objects.child_of(self).live().public() \
        .select_related('listing_image')
    extra_url_params = ''
    error_message=False
    filter_categories_raw = request.GET.get('categories')
    filter_categories = False
    filter_name = request.GET.get("name")
    
    if filter_categories_raw:
        filter_categories = []
        filter_categories_raw = filter_categories_raw.split(",")
        for fc in filter_categories_raw:
            try:
                value = int(fc)
                filter_categories.append(value)
            except ValueError:
                filter_categories = False
                error_message = "Invalid category value"

    if filter_categories:
        for filter_category in filter_categories:
            recipes = recipes.filter(categories__category=filter_category)
    
    if filter_name:
        recipes = recipes.search(filter_name, recipes) <---- issue here
        
    if not filter_name:
        filter_name=""

我添加的我的 RecipePage 模型

search_fields = BasePage.search_fields + [
    index.SearchField('title'),
]

现在当我这样做搜索时,

recipes = recipes.search(filter_name, recipes)

它给了我错误

无法将“RecipePage”对象隐式转换为 str

当我这样做时

recipes = recipes.search(filter_name, recipes.title) or recipes = recipes.search(filter_name, recipes.objects)

它给了我

“PageQuerySet”对象没有属性“title”

我被石头砸死了。我究竟做错了什么?

4

1 回答 1

0

当您查看在Wagtail 中搜索的文档时,您会发现在 Wagtail 中有两种搜索方式,我认为您混淆了。第一个是最明显的,只需将搜索词传递给 QuerySet 上的搜索方法。第二种是将搜索词和 QuerySet 传递给 search_backend 的搜索方法。

在您的情况下:

从您的search通话中删除 QuerySet:

 recipes = recipes.search(filter_name)

或者:

将搜索词和 QuerySet 传递给 search_backend:

从 wagtail.search.backends 导入 get_search_backend

s = get_search_backend()
s.search(filter_name, recipes)
于 2020-08-06T13:40:41.913 回答