我想通过使用haystack + whoosh将搜索添加到网站。我通过手动完成了所有工作。 我的模特:
class News(models.Model):
title = models.CharField(max_length=250)
slug = models.SlugField('URL', unique=True)
article = RichTextUploadingField()
head_pic= models.ImageField(u'Заглавное изображение', upload_to='head_news_photo_store',blank=True, null=True)
pub_date = models.DateTimeField('Дата публикации',auto_now_add=True)
category = models.ForeignKey(Category)
author = models.ForeignKey(settings.AUTH_USER_MODEL)
is_top_news = models.BooleanField(u'Сделать топ новостью?', default=False)
is_important = models.BooleanField(u'Добавить в слайдер?', default=False)
is_main = models.BooleanField(u'Добавить в главное?', default=False)
我的search_indexes.py在这里:
from haystack import indexes
import datetime
from .models import News
class NewsIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
category = indexes.CharField(model_attr='category')
def get_model(self):
return News
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())
当我试图让搜索请求收到下一个错误时:
Traceback:
File "C:\Python 3.5\lib\site-packages\haystack\query.py" in post_process_results
213. index = ui.get_index(model)
File "C:\Python 3.5\lib\site-packages\haystack\utils\loading.py" in get_index
308. raise NotHandled('The model %s is not registered' % model_klass)
During handling of the above exception (The model None is not registered), another exception occurred:
File "C:\Python 3.5\lib\site-packages\django\core\handlers\base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "C:\Python 3.5\lib\site-packages\django\core\handlers\base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python 3.5\lib\site-packages\haystack\views.py" in __call__
53. return self.create_response()
File "C:\Python 3.5\lib\site-packages\haystack\views.py" in create_response
133. (paginator, page) = self.build_page()
File "C:\Python 3.5\lib\site-packages\haystack\views.py" in build_page
110. self.results[start_offset:start_offset + self.results_per_page]
File "C:\Python 3.5\lib\site-packages\haystack\query.py" in __getitem__
272. self._fill_cache(start, bound)
File "C:\Python 3.5\lib\site-packages\haystack\query.py" in _fill_cache
191. to_cache = self.post_process_results(results)
File "C:\Python 3.5\lib\site-packages\haystack\query.py" in post_process_results
219. loaded_objects[model] = model._default_manager.in_bulk(models_pks[model])
Exception Type: AttributeError at /search/
Exception Value: 'NoneType' object has no attribute '_default_manager'
本地大战:
result <SearchResult: news.news (pk='2')>
ui <haystack.utils.loading.UnifiedIndex object at 0x03EDC3F0>
results [<SearchResult: news.news (pk='2')>]
loaded_objects {}
model None
to_cache []
models_pks {None: ['2']}
self Error in formatting: AttributeError: 'NoneType' object has no attribute '_default_manager'
我的新闻模式有什么问题?
答案 0 :(得分:1)
如果您使用的是Django 1.9,最新版本的Haystack(v2.3.2)不支持它。但我认为这个特殊问题已经修复并合并到主分支中,所以希望Django 1.9支持即将推出。