什么RealtimeSearchIndex有时不更新我保存的对象

什么RealtimeSearchIndex有时不更新我保存的对象

本文介绍了Haystack-为什么RealtimeSearchIndex有时不更新我保存的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Haystack和Whoosh与Django一起使用

I'm using Haystack and Whoosh with Django

在search_index.py中,我有这个

Within search_index.py I have this

class PageIndex(RealTimeSearchIndex):
    text = CharField(document=True, use_template=True)
    creator = CharField(model_attr='creator')
    created = DateTimeField(model_attr='created')
    org = CharField(model_attr='organisation')

site.register(Page, PageIndex)

我的模板如下

{{ object.name }}
{{ object.description }}
{{ object.template|striptags }}
{% for k,v in object.get_variables.items %}
{{ v }}
{% endfor %}

如果我保存了更新后的页面名称或说明,然后立即更新,并将模板中来自get_variables.items的变量包括在内。但是,如果我只更新变量,那么它就不会更新。

If I save the Page with an updated name or description then it updates straight away and includes the variables from get_variables.items in the template. However if I update just the variable then it doesn't update.

是因为变量是与之相关的另一个对象,即使我保存在同一页面上它没有拿起对页面的更改?如果是这样,当我更新相关对象时如何强制更新Page项目?

Is it because variable is another object that's related to it and even though I am saving on the same page it does not pick up a change to the Page? If so how do I force to update the Page item when I'm updating related objects?

推荐答案

我同意,但我认为这里最简单的解决方案是将侦听器附加到相关模型的post_save信号(请参见)

I concur with Daniel Hepper, but I think the easiest solution here is to attach a listener to your related model's post_save signal (see https://docs.djangoproject.com/en/dev/topics/signals/) and in that, reindex the model.

例如,在myapp / models.py中,给定模型MyRelatedModel,它具有MyModel的外键

E.g, in myapp/models.py, given model MyRelatedModel which has a foreignkey to MyModel

from myapp.search_indexes import MyModelIndex

def reindex_mymodel(sender, **kwargs):
    MyModelIndex().update_object(kwargs['instance'].mymodel)
models.signals.post_save.connect(reindex_mymodel, sender=MyRelatedModel)

这篇关于Haystack-为什么RealtimeSearchIndex有时不更新我保存的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 10:10