草垛没有索引我的多值(haystack isn't indexing my multival

2019-09-17 08:57发布

我试图让一个MultiValueField被编入索引,但它只是不工作。 以下是我有:

class Public_PollIndex(SearchIndex):
    text = CharField(model_attr='question', document=True, use_template=True)
    date_created = DateTimeField(model_attr='date_created')
    choices = MultiValueField()

    def get_model(self):
        return Public_Poll

    def prepare_choices(self, obj):
        # For some silly reason we get (u"choice",) instead of just u"choice"
        # So we unpack...
        c = [ str(c) for (c,) in obj.choice_set.values_list('choice') ]
        return c

    def index_queryset(self):
        return self.get_model().objects.filter(date_created__lte=datetime.datetime.now())

然后,我有模板:

{{ object.question }}
{{ object.date_created }}
{{ object.choices }}

与调试器步进通过prepare_choices不会返回类似['foo', 'bar']

但是,当我看着Solr的或Public_PollIndex(Public_Poll.objects.get(id=1)).load_all_queryset()我没有看到choices字段建立索引,但其他两个。

Answer 1:

你如何检查SearchQuerySetPublic_PollIndex(Public_Poll.objects.get(id=1)).load_all_queryset()返回模型QuerySet ,而不是SearchQuerySet

尝试

SearchQuerySet()[0].text
SearchQuerySet()[0].choices

此外,在模板中,渲染for循环的选择

{% for choice in object.choices %}
{{ choice }}
{% endfor %}

此外,

return obj.choice_set.values_list('choice', flat=True)

# instead of
c = [ str(c) for (c,) in obj.choice_set.values_list('choice') ]
return c


文章来源: haystack isn't indexing my multivalue