2012-05-19 67 views
2

我試圖讓一個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字段索引,但其他兩個是。

回答

3

如何檢查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 
+0

衛生署!這是一個合法的模板文件!當然!好吧,現在我只是覺得很傻... – Sandro

+0

如果使用elasticsearch,obj.choice_set.values_list('choice',flat = True) 將拋出一個elasticsearch.exceptions.SerializationError,因爲返回的對象是QuerySet類型的。索引器因TypeError失敗(「無法序列化。解決方案:return list(obj.choice_set.values_list('choice',flat = True)) – user1255933