Finding haystacks in many areas does not work

I am trying to run a search in a model with many fields, and I want to filter the search in this field.

here is my current code:

search_indexes.py

class ListingInex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) business_name = indexes.CharField(model_attr='business_name') category = indexes.MultiValueField(indexed=True, stored=True) city = indexes.CharField(model_attr='city') neighborhood= indexes.CharField(model_attr='neighborhood') state = indexes.CharField(model_attr='state') address = indexes.CharField(model_attr='address') zip_code = indexes.CharField(model_attr='zip_code') phone_number = indexes.CharField(model_attr='phone_number') def get_model(self): return listing def index_queryset(self, using=None): return self.get_model().objects.all() def prepare_category(self, obj): return [category.name for category in obj.category_set.all()] 

listing_text.txt

 {{ object.business_name }} {{ object.state }} {{object.city}} {{object.zip_code}} {{object.phone_number}} {{object.neighborhood}} {% for category in obj.category.all %} {{ category.name }} {% endfor %} 

I am trying to do a simple search, for example:

 search_results = SearchQuerySet().filter(category=query_text).filter 

it returns an empty list. Thanks in advance.

+6
source share
1 answer

Reading the code I think the problem is here:

 {% for category in obj.category.all %} {{ category.name }} {% endfor %} 

The category is not an object; it is a MultiValueField. Maybe if you try something like:

 {% for category in object.category.all %} {{ category.name }} {% endfor %} 

object.category.all instead of obj.category.all. I am not sure if this is the solution, but I am sure that the error can be in these three lines.

+2
source

Source: https://habr.com/ru/post/955548/


All Articles