haystack whoosh and multi language django site

109 views
Skip to first unread message

burak.k...@gmail.com

unread,
Apr 7, 2013, 10:16:31 AM4/7/13
to who...@googlegroups.com

I am using haystack with whoosh backend in my django project. And my models are multilingual with the modeltranslation module, it creates automatic fields like title_tr, title_en for a field named title...

I am trying to make searches aware of selected language. Having searched the net and write the below lines but it is not working for the title_tr, entry_tags_tr fields... Do you have a better solution or example on that?

#search_indexes.py

from haystack import indexes
from aksak.blog.models import Entry

class EntryIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(model_attr='descr_en', document=True, use_template=True)
    text_tr = indexes.CharField(model_attr='descr_tr')
    title_en = indexes.CharField(model_attr='title_en')
    title_tr = indexes.CharField(model_attr='title_tr')
    tags = indexes.MultiValueField(indexed=True, stored=True, model_attr='entry_tags')

    def get_model(self):
        return Entry


    # haystackCustomQuery.py  !!! IN URLS.PY I AM USING THIS CUSTOM VIEW <-------------

    from django.conf import settings
    from django.utils.translation import get_language
    from haystack.query import SearchQuerySet, DEFAULT_OPERATOR

    class MlSearchQuerySet(SearchQuerySet):
        def filter(self, **kwargs):
            if 'content' in kwargs:
                kwd = kwargs.pop('content')
                currentLngCode = str(get_language())
                lngCode = settings.LANGUAGE_CODE
                if currentLngCode == lngCode: 
                    kwdkey = "text" 
                    kwargs[kwdkey] = kwd
                else:
                    kwdkey = "text_%s" % currentLngCode
                    kwargs[kwdkey] = kwd
Reply all
Reply to author
Forward
0 new messages