gpt4 book ai didi

django - 从Haystack搜索结果中获取Django CMS插件信息

转载 作者:行者123 更新时间:2023-12-02 23:58:20 26 4
gpt4 key购买 nike

我的搜索结果页面也需要显示有关找到查询的插件的信息。我发现this question也有类似的问题,但我不仅需要内容,还需要了解有关插件的信息-即它叫什么,它在页面上的位置以及其他内容。基本上,我想引用查询所在的插件,但是我只能找到有关页面和标题的信息。我无法在SearchQuerySet对象及其附近的任何地方找到它-在Haystack的文档中也找不到它。有可能吗?

我正在使用的堆栈:Elasticsearch 2.4,django-haystack 2.8,aldryn-search 1.0(用于CMS索引)。

最佳答案

我最终为CMSPlugins编写了一个新索引。不知道我的代码使用了多少,但也许会帮助别人。

from django.conf import settings

from aldryn_search.helpers import get_plugin_index_data
from aldryn_search.utils import clean_join, get_index_base
from cms.models import CMSPlugin


class CMSPluginIndex(get_index_base()):
haystack_use_for_indexing = True
index_title = True

object_actions = ('publish', 'unpublish')

def get_model(self):
return CMSPlugin

def get_index_queryset(self, language):
return CMSPlugin.objects.select_related(
'placeholder'
).prefetch_related(
'placeholder__page_set'
).filter(
placeholder__page__publisher_is_draft=False,
language=language
).exclude(
plugin_type__in=settings.HAYSTACK_EXCLUDED_PLUGINS
).distinct()

def get_search_data(self, obj, language, request):
current_page = obj.placeholder.page
text_bits = []

plugin_text_content = self.get_plugin_search_text(obj, request)
text_bits.append(plugin_text_content)

page_meta_description = current_page.get_meta_description(fallback=False, language=language)

if page_meta_description:
text_bits.append(page_meta_description)

page_meta_keywords = getattr(current_page, 'get_meta_keywords', None)

if callable(page_meta_keywords):
text_bits.append(page_meta_keywords())

return clean_join(' ', text_bits)

def get_plugin_search_text(self, base_plugin, request):
plugin_content_bits = get_plugin_index_data(base_plugin, request)
return clean_join(' ', plugin_content_bits)

def prepare_pub_date(self, obj):
return obj.placeholder.page.publication_date

def prepare_login_required(self, obj):
return obj.placeholder.page.login_required

def get_url(self, obj):
parent_obj = self.ancestors_queryset(obj).first()

if not parent_obj:
return obj.placeholder.page.get_absolute_url()

return # however you get the URL in your project

def get_page_title_obj(self, obj):
return obj.placeholder.page.title_set.get(
publisher_is_draft=False,
language=obj.language
)

def ancestors_queryset(self, obj):
return obj.get_ancestors().filter(
plugin_type=# Some plugins that I wanted to find
).order_by(
'-depth'
)

def get_title(self, obj):
parent_obj = self.ancestors_queryset(obj).first()

if not parent_obj:
return self.get_page_title_obj(obj).title

return # get title from parent obj if you want to

def prepare_site_id(self, obj):
return obj.placeholder.page.node.site_id

def get_description(self, obj):
return self.get_page_title_obj(obj).meta_description or None

关于django - 从Haystack搜索结果中获取Django CMS插件信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51400282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com