模型类型上的Django Haystack构面 [英] Django Haystack faceting on the model type

查看:77
本文介绍了模型类型上的Django Haystack构面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据返回的不同model_names(类)对结果进行分析.有没有简单的方法可以做到这一点?

I want to facet the results based on the different model_names (classes) returned. Is there an easy way to do this?

推荐答案

您是否尝试添加带有此信息的SearchIndex字段?例如

Have you tried adding a SearchIndex field with this information? E.g.

class NoteIndex(SearchIndex, indexes.Indexable):
    title = CharField(model_attr='title')
    facet_model_name = CharField(faceted=True)

    def get_model(self):
        return Note

    def prepare_facet_model_name(self, obj):
        return "note"


class MemoIndex(SearchIndex, indexes.Indexable):
    title = CharField(model_attr='title')
    facet_model_name = CharField(faceted=True)

    def get_model(self):
        return Memo

    def prepare_facet_model_name(self, obj):
        return "memo"

依此类推,只需为每个搜索索引返回不同的字符串即可.您也可以创建一个mixin并返回get_model返回的模型的名称.

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.

假设您已将此字段添加到每个SearchIndex定义中,只需

Presuming you've added this field to each of your SearchIndex definitions, just chain the facet method to your results.

results = form.search().facet('facet_model_name')

现在facet_counts方法将返回一个包含多面字段的字典,并为每个小面值(在本例中为模型名称)计算结果.

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.

请注意,此处的字段被详细标记,以免与Haystack添加的字段model_name发生冲突.它不是多面的,我不确定复制它是否会引起冲突.

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.

这篇关于模型类型上的Django Haystack构面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆