问题描述
我想要获得一个 MultiValueField
被编入索引,但它只是不工作。这是我有的:
I'm trying to get a MultiValueField
to be indexed, but it's just not working. Here is what I have:
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']
Stepping through with the debugger prepare_choices
DOES return something like ['foo', 'bar']
但是当我查看solr或 Public_PollIndex(Public_Poll.objects.get(id = 1))。load_all_queryset()
我没有看到
But when I look into solr or
Public_PollIndex(Public_Poll.objects.get(id=1)).load_all_queryset()
I don't see the choices
field indexed, but the other two are.
推荐答案
如何查看
返回模型 SearchQuerySet
? public_PollIndex(Public_Poll.objects.get(id = 1))。load_all_queryset() QuerySet
而不是 SearchQuerySet
How do you check the SearchQuerySet
? Public_PollIndex(Public_Poll.objects.get(id=1)).load_all_queryset()
returns model QuerySet
instead of SearchQuerySet
尝试
SearchQuerySet()[0].text
SearchQuerySet()[0].choices
另外,在模板中,在forloop中呈现选择
Also, in the template, render choices in forloop
{% 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
这篇关于干草堆没有索引我的多值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!