[英]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.