简体   繁体   English

干草堆简单搜索。 没有物体显示

[英]Haystack simple search. No objects displayed

I am trying to add djangohaystack simple backend into my django app. 我正在尝试将djangohaystack simple backend添加到我的django应用程序中。 I've tried following the tutorial but none of the objects in Person are being rendered in the template. 我尝试按照教程进行操作,但模板中未呈现Person中的任何对象。 I have no idea what I might have missed. 我不知道我可能错过了什么。 The {% if query %} condition is satisfied but the next for loop is not satisfied and No results to display is displayed. 满足{% 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 %}

This is all that I have . 这就是我所拥有的。 I know simplebackend is not supposed to do an extensive search but it must return results after exact name search and I need to deploy this on heroku and heroku only supports search engines as pricey add-ons.Any idea if their is something that I might have missed? 我知道simplebackend不应进行广泛的搜索,但必须在精确名称搜索后返回结果,我需要在heroku上部署它,并且heroku仅支持搜索引擎作为昂贵的附加组件。错过了吗?

/*********************edit 1********************/ / *********************编辑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) 

full traceback - 完整追溯-

 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'

try to test 尝试测试

# -*- 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()

I looked in haystack sources, get_model moved to apps in django19 this is working example 我查看了干草堆源代码,将get_model移到了django19中的应用程序中, 这是可行的示例

I had a similar problem when searching with filters (using multiple index models). 使用过滤器(使用多个索引模型)进行搜索时,我遇到了类似的问题。 Fixed the issue by adding the below code in forms.py (github link to django-haystack/haystack/forms.py https://github.com/django-haystack/django-haystack/blob/master/haystack/forms.py ) 通过在Forms.py中添加以下代码来解决此问题(github链接至django-haystack / haystack / forms.py https://github.com/django-haystack/django-haystack/blob/master/haystack/forms.py

from haystack.utils.app_loading import haystack_get_model

For Simple Backend...This is My View.py 对于简单的后端...这是我的View.py

from haystack.utils import Highlighter
from haystack.query import SearchQuerySet
def search_title(request):
    query = request.POST.get("search_text",'&nbsp')
    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})

i Think Problem lies in your view part.Modifying your view to render code to some custom template will solve this. 我认为问题出在您的视图部分。修改视图以将代码呈现到某些自定义模板将解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM