我正在尝试将djangohaystack
simple backend
添加到我的django应用中。我已经尝试过按照教程,但Person
中没有任何对象在模板中呈现。我不知道我可能错过了什么。满足{% if query %}
条件,但不满足下一个for循环并显示No results to display
。
models.py
class Person(models.Model):
person_name = models.CharField(max_length=200)
score = models.FloatField()
search_indexes.py
class PersonIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
person_name = indexes.CharField(model_attr='person_name')
def get_model(self):
return Person
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all()
site.register(Person, PersonIndex)
note_text
{{ object.person_name }}
search.html
{% if query %}
<h3>Results</h3>
{% for result in page.object_list %}
<p>
<a href="{{ result.object.get_absolute_url }}">{{ result.object.person_name }}</a>
</p>
{% empty %}
<p>No results found.</p>
{% endfor %}
{% endif %}
这就是我的全部。我知道simplebackend
不应该做广泛的搜索,但它必须在完全名称搜索后返回结果,我需要在heroku上部署它,而heroku只支持搜索引擎作为昂贵的附加组件。任何想法,如果他们是什么的我可能错过了吗?
/ *********************编辑1 ******************** /
class PersonDetail(generics.RetrieveUpdateDestroyAPIView):
serializer_class = PersonSerializer
permission_classes = (IsAuthenticatedOrReadOnly,)
def get_object(self):
person_id = self.kwargs.get('pk',None)
return Movie.objects.get(pk=person_id)
完全追溯 -
AttributeError at /search/
'module' object has no attribute 'get_model'
Exception Location: C:\Users\final\lib\site-packages\haystack\forms.py in get_models, line 110
Traceback:
File "C:\Users\final\lib\site-packages\django\core\handlers\base.py" in get_response
149.response = self.process_exception_by_middleware(e, request)
File "C:\Users\final\lib\site-packages\django\core\handlers\base.py" in get_response
147.response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\final\lib\site-packages\haystack\views.py" in __call__
51.self.results = self.get_results()
File "C:\Users\final\lib\site-packages\haystack\views.py" in get_results
91.return self.form.search()
File "C:\Users\final\lib\site-packages\haystack\forms.py" in search
116.return sqs.models(*self.get_models())
File "C:\Users\final\lib\site-packages\haystack\forms.py" in get_models
110.search_models.append(models.get_model(*model.split('.')))
Exception Type: AttributeError at /search/
Exception Value: 'module' object has no attribute 'get_model'
答案 0 :(得分:0)
尝试测试
# -*- coding: utf-8 -*-
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "[look at manage.py]")
import django
from django.core.management import call_command
from haystack.query import SearchQuerySet
django.setup()
call_command('rebuild_index')
print SearchQuerySet().all()
我查看了干草堆来源,get_model转移到django19中的应用程序 this is working example
答案 1 :(得分:0)
使用过滤器搜索时遇到了类似的问题(使用多个索引模型)。通过在forms.py中添加以下代码解决了这个问题(github链接到django-haystack / haystack / forms.py https://github.com/django-haystack/django-haystack/blob/master/haystack/forms.py)
line = "NOVEL_SERIES, 3256432, 8, 1, 2.364, 4.5404, 9.8341"
types=[str,int,int,int,float,float,float]
key, id, xval, yval, est1, est2, est3 = [f(x) for (f,x) in zip(types,line.split(', '))]
>>> [key, id, xval, yval, est1, est2, est3]
['NOVEL_SERIES', 3256432, 8, 1, 2.364, 4.5404, 9.8341]
>>> key
'NOVEL_SERIES'
答案 2 :(得分:0)
For Simple Backend ...这是我的View.py
from haystack.utils import Highlighter
from haystack.query import SearchQuerySet
def search_title(request):
query = request.POST.get("search_text",' ')
food = SearchQuerySet().models(Food).filter(content__exact = query)
veg = SearchQuerySet().models(Veg).using('default').autocomplete(content_auto = query).highlight()
beverage = SearchQuerySet().models(Beverage).autocomplete(content_auto = query)
food = Highlighter(food)
return render(request,'food/products.html',{'food':food,'veg':veg,'beverage':beverage,'query':query})
我认为问题在于您的视图部分。修改视图以将代码呈现到某个自定义模板将解决此问题。