python - Querying a second model with django haystack -
python - Querying a second model with django haystack -
i add together field sec model django-haystack query. have 2 models have next structure:
class product(models.model): created_date = models.datefield(auto_now_add=true) name = models.charfield(max_length=254) summary = models.charfield(null=true, blank=true, max_length=255) .... class color(models.model): product_color = models.charfield(max_length=256, blank=true, null=true) created_date = models.datefield(auto_now_add=true) slug = models.slugfield(max_length=254) product = models.foreignkey('product')
i have next search_index.py:
from django.utils import timezone haystack import indexes .models import product .models import color class productindex(indexes.searchindex, indexes.indexable): text = indexes.charfield(document=true, use_template=true) def get_model(self): homecoming product def index_queryset(self, using=none): """used when entire index model updated.""" homecoming self.get_model().objects.filter( created_date__lte=timezone.now())
how can add together color
model's product_color
search index if includes partial portion of product_color
in search query homecoming product
has foreignkey relationship color?
use multivaluefield
store colors associated product:
product_colors = indexes.multivaluefield()
prepare it:
def prepare_product_colors(self, obj): homecoming [o.product_color o in obj.color_set.all()]
and utilize field straight filter product color. or if don't want utilize search on specific field rather auto query append product colors final indexed text:
def prepare(self, obj): prepared_data = super(slateupindex, self).prepare(obj) prepared_data['text'] += ' '.join(self.prepare_product_colors(obj)) homecoming prepared_data
instead of doing above things add together colors in template search/indexes/{app_label}/{model_name}_{field_name}.txt
:
{% color in object.color_set.all %} {{ color.product_color }} {% endfor %}
python django elasticsearch django-haystack
Comments
Post a Comment