건초 더미 검색을 내 웹 사이트에 설정했습니다. 검색은 잘 작동하고 정말 좋아합니다. 추가 컨텍스트를 추가하는 데 문제가 있습니다. 3 가지 모델의 개체를 내 템플릿으로 "푸시"하고 싶습니다. 첫 번째 개체는 내 검색 결과이고 두 번째 개체는 추가 검색 결과입니다. 내 질문은 : 어떻게 다른 모델에서 개체를 전달할 수 있습니다. 여기 내 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)
검색 중이 잘 작동하지만 나는 모든 카테고리 또한 모든 페이지의 목록의 목록을 갖고 싶어 (I는 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
그러나이 코드는 작동하지 않습니다 내가 어떻게 easly 내 검색 결과에 추가 모델을 구현하는 도움을 감사하는 아무 생각이
:..! 't 작업 나는 서브 클래스와 두 번째 시도