0
votes

This is my first post here, so if I did something I shouldn't say it and I will change it. I am currently developing a project with django. I got stuck several times but were able to help myself with the great questions and answers here at stackoverflow. I am now at a point where I can't help myself with that alone so here is the question:

I am implementing search with haystack and elasticsearch. All indexing works and I get the results I want. Ordering and filtering works too. Now I want to be able to show all results by default and then filter or order them without a query. I already tried everything at Django Haystack - Show results without needing a search query?

It doesn't seem to work for me though to subclass the Searchform.

My Code:

forms.py:

class CustomSearchForm(SearchForm):

    def no_query_found(self):
        print('no query')
        return self.searchqueryset.all()

search.html:

 <form method="get" action=".">
    {{ form.as_table }}
    <button>hit</button>
    </form>
...
        {% if query or page_obj %}
            {% for result in page_obj.object_list %}
                    <div class="col-md-2">
                        <div class="cell">
                            <div class="description">
                                {{ result.object.Studiengang }}
                            </div>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="cell">
                            <div class="description">
                                <p>{{ result.object.Bildungsinstitut }}</p>
...
                {% empty %}
                    <p>No results found.</p>
                {% endfor %}

            {% if page_obj.has_previous or page_obj.has_next %}
                <div>
                    {% if page_obj.has_previous %}
                        <a href="?
                                {% if urlquery %}{% for key, value in urlquery.items %}&{{ key }}={{ value }}{% endfor %}{% endif %}&amp;page={{ page_obj.previous_page_number }}">{% endif %}&laquo;
                    Zurück{% if page_obj.has_previous %}</a>{% endif %}
                    |
                    {% if page_obj.has_next %}
                        <a href="?
                                {% if urlquery %}{% for key, value in urlquery.items %}&{{ key }}={{ value }}{% endfor %}{% endif %}&amp;page={{ page_obj.next_page_number }}">{% endif %}
                    Weiter &raquo;{% if page_obj.has_next %}</a>{% endif %}
                </div>
            {% endif %}
        {% else %}
            {# Show some example queries to run, maybe query syntax, something else? #}
        {% endif %}

views.py:

class MySearchView(SearchView):
    """My custom search view."""

def get_queryset(self):
    urlquery = self.request.GET.copy()
    queryset = super(MySearchView, self).get_queryset()
    if urlquery:
        if 'filterbildungsart' in urlquery:
            queryset = queryset.filter(Bildungsart=urlquery['filterbildungsart'])
        if 'filterfachbereich' in urlquery:
            queryset = queryset.filter(Fachbereich=urlquery['filterfachbereich'])
        if 'order_up_Studiengang' in urlquery:
            return queryset.order_by('Studiengang')
        elif 'order_down_Studiengang'in urlquery:
            return queryset.order_by('-Studiengang')
        elif 'order_up_Bildungsinstitut' in urlquery:
            return queryset.order_by('Bildungsinstitut')
        elif 'order_down_Bildungsinstitut'in urlquery:
            return queryset.order_by('-Bildungsinstitut')
        else:
            return queryset
    else:
        return queryset

I've shortened the html-code for visibility and hope everything relevant is there.

What happens at different urls:

All results - 127.0.0.1:8000/de/search/

No results - 127.0.0.1:8000/de/search/?&order_up_Bildungsart

No results - 127.0.0.1:8000/de/search/?&filterbildungsart=Bachelor

No results - 127.0.0.1:8000/de/search/?q=

All results - 127.0.0.1:8000/de/search/?q=Deutschland (q=Deutschland is a query which all indexed items have in common right now)

All results - 127.0.0.1:8000/de/search/?q=Deutschland&filterfachbereich=Ingenieurwissenschaften&order_up_Bildungsart (all results are ordered and filtered as intended)

My Problem is also that I seem to be not able to get into the code from my form. The Print statement doesn't appear in the terminal.

I've also tried the other approaches in the forementioned question to no avail.

Thanks in Advance for your help.

2

2 Answers

0
votes

After putting my problem aside and getting to another one I finally found my mistake. My custom view did not use my custom form because I didn't add it in the urls.py. By giving the form_class attribute to my view via urls.py everything is shown without a query:

urls.py:

url(r'^search/$', views.MySearchView.as_view(form_class=CustomSearchForm), name='search_view'),
0
votes

Instead of using all() function, use load_all() function. As I observed that all() takes more time to return the results when the amount is high and load_all() gives the results very quickly as said in the function's description: """Efficiently populates the objects in the search results.""".

    def no_query_found(self):
        return self.searchqueryset.load_all()