模型类型上的 Django Haystack 分面
我想根据返回的不同模型名称(类)对结果进行分面。有没有简单的方法可以做到这一点?
I want to facet the results based on the different model_names (classes) returned. Is there an easy way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过添加包含此信息的
SearchIndex
字段?例如,依此类推,只需为每个搜索索引返回不同的字符串。您还可以创建一个 mixin 并返回
get_model
返回的模型的名称。假设您已将此字段添加到每个
SearchIndex
定义中,只需 将facet
方法链接到您的结果。现在,
facet_counts
方法将返回一个字典,其中包含分面字段和每个分面值的结果计数(在本例中为模型名称)。请注意,此处的字段被详细标记,以避免与 Haystack 添加的字段
model_name
可能发生冲突。它不是多面的,我不确定复制它是否会导致冲突。Have you tried adding a
SearchIndex
field with this information? E.g.And so on, simply returning a different string for each search index. You could also create a mixin and return the name of the model returned by
get_model
too.Presuming you've added this field to each of your
SearchIndex
definitions, just chain thefacet
method to your results.Now the
facet_counts
method will return a dictionary with the faceted fields and count of results for each facet value, in this case, the model names.Note that the field here is labeled verbosely to avoid a possible conflict with
model_name
, a field added by Haystack. It's not faceted, and I'm not sure if duplicating it will cause a conflict.如果您只想过滤模型类型,可以使用 模型搜索表单
If you just want to filter on the model type, you can use the ModelSearchForm
文档对此有一个非常好的演练。
您至少需要:
faceted=True
添加到model_names
字段的参数中。.facet('model_names')
添加到您想要分面的任何 SearchQuerySet 中。对问题的更多解释将使答案更完整。
The Docs have a really good walk-through for this.
The minimum you'll need:
faceted=True
to the params of yourmodel_names
field..facet('model_names')
to whatever SearchQuerySet you're wanting to facet.More explanation on the question would enable a more complete answer.