为 Haystack 模板指定备用路径

Specifying an alternate path for a Haystack template

The Haystack documentation states that:

Additionally, 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 index. You’ll need to create a new template inside your template directory called search/indexes/myapp/note_text.txt

遗憾的是,没有关于如果您想要一个不同名称的模板该怎么做的信息。

是否可以指定 haystack 文档模板的路径?

该死,我找了一个星期。

Its listed under the SearchField API documentation,这是搜索索引中实际字段的超类。

SearchField.template_name

Allows you to override the name of the template to use when preparing data. By default, the data templates for fields are located within your TEMPLATE_DIRS under a path like search/indexes/{app_label}/{model_name}_{field_name}.txt. This option lets you override that path (though still within TEMPLATE_DIRS).

Example:

bio = CharField(use_template=True, template_name='myapp/data/bio.txt')

You can also provide a list of templates, as loader.select_template is used under the hood.

Example:

bio = CharField(use_template=True, template_name=['myapp/data/bio.txt', 'myapp/bio.txt', 'bio.txt'])