Indexing a method return (depending on Internationalization)

Posted by Hedde on Stack Overflow See other posts from Stack Overflow or by Hedde
Published on 2012-12-18T17:02:00Z Indexed on 2012/12/18 17:02 UTC
Read the original article Hit count: 161

Filed under:
|
|

Consider a django model with an IntegerField with some choices, e.g.

COLORS = (
    (0, _(u"Blue"),
    (1, _(u"Red"),
    (2, _(u"Yellow"),
)

class Foo(models.Model):
    # ...other fields...
    color = models.PositiveIntegerField(choices=COLOR, verbose_name=_(u"color"))

My current (haystack) index:

class FooIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    color = CharField(model_attr='color')

    def prepare_color(self, obj):
        return obj.get_color_display()

site.register(Product, ProductIndex)

This obviously only works for keyword "yellow", but not for any (available) translations.

Question: What's would be a good way to solve this problem? (indexing method returns based on the active language)

What I have tried: I created a function that runs a loop over every available language (from settings) appending any translation to a list, evaluating this against the query, pre search. If any colors are matched it converts them backwards into their numeric representation to evaluate against obj.color, but this feels wrong.

© Stack Overflow or respective owner

Related posts about django

Related posts about solr