0

I've hit a dead end with my django 1.9.5 project. Im running Haystack 2.4.1 and Whoosh 2.7.4 as a search-engine. Here is my poblem:

When accessing followng view:

def search_titles(request):
    resources = SearchQuerySet().autocomplete(content_auto=request.POST.get('search_text', ''))
    return render_to_response('ajax_search.html', {'resources' : resources})

with ajax_search.html looking as follows:

{% if resources.count > 0 %}
{% for resource in resources %}
    <li>{{resource.object.title}} _ {{resource.object}}</li>
{% endfor %}
{% else %}
<li>None to show</li>
{% endif %}

The search works just fine. Depending on what I search for I get the exact amount of results I should be getting, however at the line

<li>{{resource.object.title}} _ {{resource.object}}</li>

they all show up as

  • _ None

(.object.title being blank and .object being None).

In case it helps, here is my search_indexes.py:

class ResourceIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    published = indexes.DateField(model_attr='published')
    content_auto = indexes.EdgeNgramField(model_attr='title')
    def get_model(self): return Resource
    def index_queryset(self, using=None): return self.get_model().objects.all()

and resource_text.txt:

{{object.title}}
{{object.subtitle}}

The Resource-model looks like this:

class Resource(models.Model):
    authors = models.ManyToManyField(Person, related_name='resources_authored')
    title = models.CharField(max_length=300, unique=True)
    subtitle = models.CharField(max_length=300, unique=True)
    published = models.DateField('date published')

    def __str__(self): return self.title
    def get_absolute_url(self): return '/resources/%i/' % self.id
    class Meta:
        ordering = ['-published', 'title']
        get_latest_by = 'published'

Anybody got any ideas why I get None-objects when calling .object on the results in the SearchQuerySet?

Helliaca
  • 183
  • 2
  • 10
  • What do you get if you put {{resource}} or {{resource.title}} in the template? – raphv Jun 10 '16 at 16:05
  • @raphv for resource I get " with pk being a different number depending on the result. For resource.title I get None. – Helliaca Jun 10 '16 at 16:13
  • Is `Resource` a django model? Can you put the class definition in your question? – raphv Jun 10 '16 at 16:25
  • @raphv Yes, it is. I've edited my question, although I doubt it has something to do with the underlying problem. – Helliaca Jun 10 '16 at 16:32

0 Answers0