issues while searching models in wagtail django

267 Views Asked by At

I'm having a bit of crisis in searching models in wagtail django

This is my code for model objects

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=""

My RecipePage model I added

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

Now when I do this for searching,

recipes = recipes.search(filter_name, recipes)

it gives me error

Can't convert 'RecipePage' object to str implicitly

When I do this

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

It gives me

'PageQuerySet' object has no attribute 'title'

I'm stoned. What am I doing wrong?

1

There are 1 best solutions below

1
Nico Griffioen On

When you look at the docs for searching in Wagtail you'll see there are two ways to search in Wagtail, that I think you are mixing up. The first is the most obvious, by simply passing a search term to the search method on a QuerySet. The second is by passing a search term and a QuerySet to the search_backend's search method.

In your case either:

Remove the QuerySet from your search call:

 recipes = recipes.search(filter_name)

or:

Pass the search term and QuerySet to the search_backend:

from wagtail.search.backends import get_search_backend

s = get_search_backend()
s.search(filter_name, recipes)