简体   繁体   中英

Haystack simple search. No objects displayed

I am trying to add djangohaystack simple backend into my django app. I've tried following the tutorial but none of the objects in Person are being rendered in the template. 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.

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?

/*********************edit 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

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 )

from haystack.utils.app_loading import haystack_get_model

For Simple Backend...This is My 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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