Haystack Django Tables2 Expected table or queryset

2019-07-24 17:31发布

问题:

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!

回答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,
    }


回答2:

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