简体   繁体   English

如何使用Haystack添加额外的上下文

[英]How to add extra context with Haystack

I have set up Haystack search to my website. 我已经将Haystack搜索设置到我的网站。 Searching works fine and I really like it. 搜索效果很好,我真的很喜欢。 I have a problem with adding extra context. 我在添加额外的上下文时遇到问题。 I want to "push" objects from 3 models, to my template. 我想将3种模型中的对象“推送”到我的模板中。 First object is my search result, two another should be additional. 第一个对象是我的搜索结果,另外两个应该是附加的。 My question is: how can I pass objects from another models. 我的问题是:如何传递其他模型中的对象。 Here is my search_indexes.py file: 这是我的search_indexes.py文件:

import datetime
from haystack.indexes import *
from haystack import site
from filmy.models import Video, Page, Category

class VideoIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    title = CharField(model_attr='title')
    description = CharField(model_attr='description')
    date = DateTimeField(model_attr='date')

    def index_queryset(self, using=None):
        # """Used when the entire index for model is updated."""
        return Video.objects.filter(date__lte=datetime.datetime.now())

    def extra_context(self):
        return {
            'categories': Category.objects.all().order_by('-name'),
            'list_of_pages': Page.objects.all().order_by('id'),
        }

site.register(Video, VideoIndex)

Searching works fine, but I want to have a list of all Categories and list of all Pages also (I use them in base.html template. My solution doesn't work. I tried second one with subclass: 搜索效果很好,但是我也想拥有所有类别的列表和所有页面的列表(我在base.html模板中使用它们。我的解决方案不起作用。我尝试了第二个子类:

import datetime
from haystack.indexes import *
from haystack import site
from filmy.models import Video, Page, Category

class VideoIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    title = CharField(model_attr='title')
    description = CharField(model_attr='description')
    date = DateTimeField(model_attr='date')

    def index_queryset(self, using=None):
        # """Used when the entire index for model is updated."""
        return Video.objects.filter(date__lte=datetime.datetime.now())

site.register(Video, VideoIndex)

class VideoSearchIndex(VideoIndex):
    def extra_context(self):
        extra = super(VideoSearchIndex, self).extra_context()
        extra['categories'] = Category.objects.all().order_by('-name')
        extra['list_of_pages'] = Page.objects.all().order_by('id')
        return extra

But this code also doesn't work. 但是此代码也不起作用。 I have no idea how to easly implement additional models to my search results. 我不知道如何轻松地在搜索结果中实现其他模型。 Thanks for any help! 谢谢你的帮助!

I found solution to my problem. 我找到了解决问题的办法。 I couldn't solve it with extra_context function, so I used TEMPLATE_CONTEXT_PROCESSORS to set global variables in my template. 我无法使用extra_context函数解决它,因此我使用TEMPLATE_CONTEXT_PROCESSORS模板中设置全局变量。

It's quite convenient solution because I don't need to use extra_context functions in my view to all my models. 这是一个非常方便的解决方案,因为在我的所有模型中,我都不需要使用extra_context函数。 I just set global variables in one file in one definition. 我只是在一个定义中的一个文件中设置了全局变量。 It increased readability of views.py file. 它增加了views.py文件的可读性。

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

相关问题 Django-Haystack:如何使用FacetedSearchView传递额外的上下文 - Django-Haystack: How to pass extra context with FacetedSearchView 您可以从urls.py向Django Haystack添加上下文吗? - Can you add context to Django Haystack from urls.py? 如何在基于 Class 的视图 (ListView) 中添加额外的上下文 - How to add an extra context in a Class Based View (ListView) django haystack-如何与具有某些上下文的当前视图一起使用 - django haystack - how to use with current view having some context 向管理索引页面添加额外的上下文 - Add extra context to admin index page TurboGears和反冲:如何向Raven添加额外的,按请求的上下文以获取更多的Sentry报告? - TurboGears and backlash: How to add extra, per-request context to Raven for more informative Sentry reports? 如何将相关数据添加到干草堆模型索引中? - How do I add related data to a haystack model index? 如何在 wagtail 页面中发送额外的上下文 - how to send extra context in wagtail Page DRF:如何将额外的上下文数据传递给序列化程序 - DRF: How to pass extra context data to serializers python,django,solr,haystack:编辑solr_build_schema BaseCommand.add_argument()时django模板上下文错误 - python,django, solr, haystack: django templates context error when editing solr_build_schema BaseCommand.add_argument()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM