私は自分のウェブサイトにHaystack検索を設定しました。検索はうまく機能し、私はそれが本当に好きです。コンテキストの追加に問題があります。3つのモデルからテンプレートにオブジェクトを「プッシュ」したいと思います。最初のオブジェクトは私の検索結果であり、もう2つは追加する必要があります。私の質問は、別のモデルからオブジェクトを渡すにはどうすればよいかということです。これが私の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)
検索は正常に機能しますが、すべてのカテゴリのリストとすべてのページのリストも必要です(base.htmlテンプレートで使用しています。ソリューションが機能しません。サブクラスで2番目のものを試しました。
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
しかし、このコードも機能しません。検索結果に追加のモデルを簡単に実装する方法がわかりません。助けてくれてありがとう!