django – 使用update_index –remove从Haystack / Xapian索引中
发布时间:2020-12-20 11:20:02 所属栏目:Python 来源:网络整理
导读:我正在尝试使用./manage.py update_index –remove管理命令从搜索索引中删除结果. 我想删除这些对象,而不是删除它们时,只是在它们的时候: enabled = models.BooleanField() 字段是假的 我该怎么办?我还需要准备SearchIndex吗? import datetimefrom haysta
我正在尝试使用./manage.py update_index –remove管理命令从搜索索引中删除结果.
我想删除这些对象,而不是删除它们时,只是在它们的时候: enabled = models.BooleanField() 字段是假的 我该怎么办?我还需要准备SearchIndex吗? import datetime from haystack.indexes import * from haystack import site from articles.models import Article class ArticleIndex(SearchIndex): text = CharField(document=True,use_template=True) title = CharField(model_attr='title') content = CharField(model_attr='content') def get_queryset(self): """Used when the entire index for model is updated.""" return Article.site_published_objects.filter(enabled=True) def get_updated_field(self): return 'modified' def remove_object(self): pass site.register(Article,ArticleIndex) 谢谢. 解决方法
我没有对此进行测试,但您可以通过将搜索索引模板包装在一个条件中来实现所需的效果,例如:
{# in search/indexes/yourapp/article_text.txt #} {% if object.enabled %} {# ... whatever you have now #} {% endif %} 运行./manage.py update_index时,启用了= False的文章最终将没有与之关联的数据,也不会显示在搜索中. 更新 查看SearchIndex的源代码,有一个用于从索引中删除对象的 也许可以使用以下方法触发索引删除: class ArticleIndex(SearchIndex): # ... def should_update(self,instance,**kwargs): if not instance.enabled: self.remove_object(instance,**kwargs) return instance.enabled (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 为什么heappop时间复杂度在python中是O(logn)(不是O(n))?
- python – 在Matplotlib直方图或Seaborn distplots的箱子上
- python – PyQt窗口在打开后立即关闭
- python – Tkinter Canvas将项目移动到顶层
- python – 具有SWIG未知长度数组的NumPy C扩展
- python – 如何在CentOs中将uWsgi作为服务运行?
- python读取html中指定元素生成excle文件示例
- 【Flask模版语言Jinja2】--2019-08-06 15:48:34
- python – 使用ArtistAnimation在matplotlib中制作动画
- Python字符串关键点用法