如何在Haystack的两个django应用程序中创建索引?

时间:2013-06-22 08:18:38

标签: django django-haystack

我的干草堆搜索一个django应用程序。但是如何在两个不同的应用程序上实现相同。

我试过了。在两个应用程序中处理不同的search_index.py App1

import datetime
from haystack import indexes
from app1.models import App1

 class app1Index(indexes.SearchIndex, indexes.Indexable):
    text = indexes.EdgeNgramField(document=True, use_template=True)
    app1_text = indexes.CharField(model_attr='app1_text',null=True)
    tags = indexes.CharField(model_attr='tags')

    def get_model(self):
        return App1

App2的

import datetime
from haystack import indexes
from app2.models import App2

class app2Index(indexes.SearchIndex, indexes.Indexable):
    text = indexes.EdgeNgramField(document=True, use_template=True)
    app2_title = indexes.CharField(model_attr='app2_title',null=True)

    def get_model(self):
        return App2

这是search.html

        {% for result in page.object_list %}
        <p>
                <a href="{{ result.object.get_absolute_url }}">{{ result.object.wish_text }}</a>
                <a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>

        </p>

我在两个应用中都有正确的数据模板。 APP1     {{object.app1_text}} 应用2     {{object.app2_title}}

如果我不使用App2 search_index,那么对于App1它运行正常。 此外,rebuild_index和update_index也适用于这两个应用程序。

问题:如何让它适用于这两个应用程序?

2 个答案:

答案 0 :(得分:0)

我也不完全理解你想指出的问题 所以据我所知,构建索引确实有效,这意味着app(1 | 2)索引确实找到了合适的模板。 它可能是app2的网址问题。为了进一步调查,可能会对模板感兴趣

答案 1 :(得分:0)

我删除了所有内容并重新添加。

1 search_indexes.py in first app
1 search_indexes.py in second app
and a search.html template that fetch the results for both.

和宾果这次工作完美。那是什么问题。 :)我太不知道可能是一些拼写错误。

If somebody need explaination on answer please drop a comment here :) 

我现在可以搜索多个应用。

欢迎任何其他解决方案。

接受以供将来参考。