Haystack Django Tables2 Expected table or queryset, not 'str'
Asked Answered
M

2

2

Currently using Haystack 2.1 with Tables2(Dev 0.16) to manage queries using Elastisearch. When I attempt a search I get the following error messsage: Expected table or queryset, not 'str'. Details are here

In template C:\RealPython\quorum\templates\search\search.html, error at line 20 Expected table or queryset, not 'str'.

10  <td>&nbsp;</td>
11  <td>
12  <input type="submit" value="Search">
13  </td>
14  </tr>
15  
16  </table>
17  
18  {% if query %}
19  <h3>Results</h3>
20  {% render_table table %}
21  {% for result in page.object_list %}
22  <p>
23  
24  <a href="{{ result.object.get_absolute_url }}">{{ result.object.Inducer_Name_Synonym_Abbreviation }}</a>
25  </p>
26  
27  {% empty %}
28  
29  {% endfor %}
30 

My views.py code is here:

def report(request, template='search/search.html', load_all=True, form_class=ModelSearchForm, searchqueryset=None, context_class=RequestContext, extra_context=None, results_per_page=None):
    table = ''
    query = ''
    results = EmptySearchQuerySet()

    if request.GET.get('q'):
        form = form_class(request.GET, searchqueryset=SearchQuerySet().models(Quorum_Sensing), load_all=load_all)

        if form.is_valid():
            query = form.cleaned_data['q']
            results = form.search()
            pklist = [r.pk for r in results]
            table = QuorumTable(Quorum_Sensing.objects.filter(pk__in=pklist))
            RequestConfig(request).configure(table)

    else:
        form = form_class(searchqueryset=searchqueryset, load_all=load_all)

    context = {
        'form': form,
        'table': table,
    }

    return render(request, template, context)

def quorum_list(request):
    quorum_list = QuorumTable(Quorum_Sensing.objects.filter(pk=1))
    RequestConfig(request).configure(quorum_list)
    return render(request, 'simple_list.html', {"quorum_list": Quorum_Sensing.objects.all()})

How do I fix this error message? I'm using the most recent tables2 with Django 1.62.

Thank you in advance for your help!

Manes answered 14/3, 2014 at 17:34 Comment(0)
G
1

You are getting this problem probably when request.GET.get(q) (in def report) is None. So you need to update the if else block of report function like this:

if request.GET.get('q'):
    form = form_class(request.GET, searchqueryset=SearchQuerySet().models(Quorum_Sensing), load_all=load_all)
    if form.is_valid():
        query = form.cleaned_data['q']
        results = form.search()
        pklist = [r.pk for r in results]
        table = QuorumTable(Quorum_Sensing.objects.filter(pk__in=pklist))
            RequestConfig(request).configure(table)

else:
   form = form_class(searchqueryset=searchqueryset, load_all=load_all)
   table = QuorumTavke(Quorum_Sensing.objects.none())

    context = {
        'form': form,
        'table': table,
    }
Gadolinite answered 17/4, 2015 at 7:2 Comment(0)
A
0

You're using an old version of django-tables2.

Alsacelorraine answered 1/2, 2015 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.