Django-Haystack:了解Haystack中数据模板的使用

时间:2022-03-23 23:47:44

I am new to Haystack. I cannot understand why we have to use a template to render it with the text that we want to search. More simple , why we don't have to use something like this?

我是Haystack的新手。我无法理解为什么我们必须使用模板来渲染我们想要搜索的文本。更简单,为什么我们不必使用这样的东西?

text = indexes.CharField(document=True, "and here the attributes to search")

UPDATE To be more specific Let's say that we have an app places an here a model countries. In the model i want to be searchable from haystack the fields capital and biggest_cities. So in search_indexes.py i put

更新更具体我们说我们有一个应用程序在这里放置一个模型国家。在模型中,我希望可以从干草堆中搜索字段capital和largest_cities。所以在search_indexes.py中我放了

text = indexes.CharField(document=True, use_template=True )

After make a template in the path search/indexes/places/countries_text.txt Here i put

在路径搜索/ indexes / places / countries_text.txt中创建模板后我放了

{{ object.capital }}
{{ object.biggest_cites }}

Again the question is: why we have to use a template in order to accomplish our goal?

问题是:为什么我们必须使用模板来实现我们的目标?

It wouldn't be easier to use something like

使用类似的东西并不容易

text = indexes.CharField(document=Truer, model_attr='capital',model_attr='biggest_cites')

1 个解决方案

#1


1  

Have you read this Haystack Documentation page http://django-haystack.readthedocs.org/en/latest/searchindex_api.html ?

你有没有读过这个Haystack文档页面http://django-haystack.readthedocs.org/en/latest/searchindex_api.html?

If you haven't, you must. If you have, read it again.

如果你没有,你必须。如果有,请再次阅读。

The SearchIndex API contains valuable fundamentals of how Haystack works on your project. It can also grant you a useful insight of "why you use templates to make your data searchable'.

SearchIndex API包含有关Haystack如何在您的项目中工作的宝贵基础知识。它还可以为您提供有用的洞察力,“您可以使用模板来搜索数据”。

why we have to use a template in order to accomplish our goal?

为什么我们必须使用模板来实现我们的目标?

From the Haystack Docs:

来自Haystack Docs:

"...we’re providing use_template=True on the text field. This allows us to use a data template (rather than error prone concatenation) to build the document the search engine will use in searching"

“...我们在文本字段中提供use_template = True。这允许我们使用数据模板(而不是容易出错的连接)来构建搜索引擎将用于搜索的文档”

As you can see, we can choose whether use a template or not.

如您所见,我们可以选择是否使用模板。

Ps: sorry for the late post; I hope it helps you.

Ps:对于迟到的帖子感到抱歉;我希望它对你有所帮助。

#1


1  

Have you read this Haystack Documentation page http://django-haystack.readthedocs.org/en/latest/searchindex_api.html ?

你有没有读过这个Haystack文档页面http://django-haystack.readthedocs.org/en/latest/searchindex_api.html?

If you haven't, you must. If you have, read it again.

如果你没有,你必须。如果有,请再次阅读。

The SearchIndex API contains valuable fundamentals of how Haystack works on your project. It can also grant you a useful insight of "why you use templates to make your data searchable'.

SearchIndex API包含有关Haystack如何在您的项目中工作的宝贵基础知识。它还可以为您提供有用的洞察力,“您可以使用模板来搜索数据”。

why we have to use a template in order to accomplish our goal?

为什么我们必须使用模板来实现我们的目标?

From the Haystack Docs:

来自Haystack Docs:

"...we’re providing use_template=True on the text field. This allows us to use a data template (rather than error prone concatenation) to build the document the search engine will use in searching"

“...我们在文本字段中提供use_template = True。这允许我们使用数据模板(而不是容易出错的连接)来构建搜索引擎将用于搜索的文档”

As you can see, we can choose whether use a template or not.

如您所见,我们可以选择是否使用模板。

Ps: sorry for the late post; I hope it helps you.

Ps:对于迟到的帖子感到抱歉;我希望它对你有所帮助。