2👍
✅
Use MultiValueField
which will store all colors associated with product:
product_colors = indexes.MultiValueField()
Prepare it:
def prepare_product_colors(self, obj):
return [o.product_color for o in obj.color_set.all()]
And use that field directly to filter by product color. Or If you don’t want to use search on specific field rather do an auto query then append the product colors to the final indexed text:
def prepare(self, obj):
prepared_data = super(SlateUpIndex, self).prepare(obj)
prepared_data['text'] += ' '.join(self.prepare_product_colors(obj))
return prepared_data
Instead of doing all the above things just add the colors in the template search/indexes/{app_label}/{model_name}_{field_name}.txt
:
{% for color in object.color_set.all %}
{{ color.product_color }}
{% endfor %}
0👍
Do products have multiple colors? If not, i’d FK to ‘Color’ from the ‘Product’ model.
As-is, you can add a MultiValue Field to your index and prepare the values using a ‘prepare_foo’ function that is a built-in helper a-la ‘clean_foo’ in django forms.
See the docs for more info: SearchIndex Api
For example:
colors = indexes.MultValueField()
def prepare_colors(self, obj):
return [color.product_color for color in obj.color_set.all()]
- [Answered ]-Find PID of a daemon?
- [Answered ]-Can a older version of django project work on higher version?
- [Answered ]-Get query result as a tuple to make substitution
Source:stackexchange.com