如何更改文档的名称= Django中干草堆的真实字段?

时间:2014-10-28 23:32:39

标签: python django solr django-haystack

我想使用干草堆,但我的所有型号都有" body"作为他们的文本字段名称。但它在所有型号上都是一样的。

现在我收到了这个错误:

All 'SearchIndex' classes must use the same 'text' fieldname for the 'document=True' field. Offending index is '<qna.search_indexes.QuestionIndex object at 0x2435328>'.

这是索引文件:

导入日期时间 来自haystack导入索引 来自qna.models导入问题

class QuestionIndex(indexes.SearchIndex, indexes.Indexable):
    subject = indexes.CharField(document=False, use_template=False)
    body = indexes.CharField(document=True, use_template=True, model_attr='user')
    pub_date = indexes.DateTimeField(model_attr='pub_date')

    def get_model(self):
        return Question

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())

这是唯一一个!什么是冒犯?据我所知,字段名称不必是&#34; text&#34;它只需要在每个领域都一样。但它是唯一的领域!我需要更改一些配置吗?可能是什么原因...... ??

1 个答案:

答案 0 :(得分:1)

我在大海捞针源中看到了你的错误。看起来这个字段的名称设置为https://github.com/toastdriven/django-haystack/blob/master/haystack/utils/loading.py#L154):

self.document_field = getattr(settings, 'HAYSTACK_DOCUMENT_FIELD', 'text')

稍后在该文件(https://github.com/toastdriven/django-haystack/blob/master/haystack/utils/loading.py#L222)中,它会检查以确保您的索引名称与您看到的错误匹配并爆炸,如果他们不同意:

if field_object.index_fieldname != self.document_field:
    raise SearchFieldError("All 'SearchIndex' classes must use the same '%s' fieldname for the 'document=True' field. Offending index is '%s'." % (self.document_field, index))

如果您将HAYSTACK_DOCUMENT_FIELD设置为&#34; body&#34;在你的设置中,看起来应该这样做。