简体   繁体   English

如何使用Haystack / Whoosh与Django索引外键CharField?

[英]How to index a foreign key CharField using Haystack/Whoosh with Django?

Using prepare_FOO(self, object) method, I'm trying to index a ForeignKey to get the name attribute of my tags (travel, family, ...) 使用prepare_FOO(self, object)方法,我试图索引一个ForeignKey来获取我的标签的name属性(旅行,家庭,......)

This is my model 这是我的模特

class Blog(models.Model):
    title = models.CharField(max_length=500)
    description = models.TextField(blank=True, null=True)
    tag = models.ForeignKey(Tag)
    #...

And in my search_index.py, that's what I have: 在我的search_index.py中,这就是我所拥有的:

class BlogIndex(indexes.SearchIndex, indexes.Indexable):

    text = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='title')
    description = indexes.CharField(model_attr='description')

    tag_name = indexes.CharField()
    def get_model(self):
        return Blog

    def prepare_tag_name(self, obj):
        return obj.tag.name

    def index_queryset(self, using=None):
        return self.get_model().objects.all().select_related('blog__tag')

... And my blog_text: ......还有我的blog_text:

{{ object.title }}
{{ object.description }}

Any help would be appreciated, thank you! 任何帮助将不胜感激,谢谢!

You only need to add the following in your blog_text file blog_text and rebuild your index. 您只需在blog_text文件blog_text中添加以下内容并重建索引。 and it works! 它的工作原理!

{{ object.title }}
{{ object.description }}
{{ object.tag.name }}

you don't need this in your class BlogIndex 你在班级BlogIndex不需要这个

tag_name = indexes.CharField()
def prepare_tag_name(self, obj):
    return obj.tag.name

similar answer is here Search over multiple fields a nice explanation is here haystack multiple field search In your index you should define one field with document=True , which is the document haystack will search on. 类似的答案在这里搜索多个字段一个很好的解释是在这里haystack多字段搜索在索引中你应该定义一个document=True字段,这是haystack将搜索的文件。 By convention this field is named text. 按照惯例,此字段命名为文本。 You add extra fields if you plan to do filtering or ordering on their values. 如果您计划对其值进行过滤或排序,则可以添加额外的字段。

So essentially, the fields you include in your Index class are mainly for sorting or filtering. 基本上,您在Index类中包含的字段主要用于排序或过滤。 the search fields are defined in blog_text file. 搜索字段在blog_text文件中定义。

Another solution is here which is more complicated. 这里的另一种解决方案更复杂。 haystack - how you display data from multiple models with ForeignKeys? haystack - 如何使用ForeignKeys显示来自多个模型的数据?

This is a very old question but given answer does not really answer to the problem (even this is a correct answer anyway) Pompeyo, I think haystack require (required ?) a "model_attr" arg. 这是一个非常古老的问题,但答案并没有真正回答问题(即使这是一个正确的答案)Pompeyo,我认为haystack需要(必需?)一个“model_attr”arg。 You should defined your tag_name like this : 你应该像这样定义你的tag_name:

tag_name = indexes.CharField(model_attr='tag')

And your prepare_foo method : 而你的prepare_foo方法:

def prepare_tag_name(self, obj):
    return '' if not obj.tag else obj.tag.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM