A
A
AlexandrBirukov2017-08-25 10:50:25
Django
AlexandrBirukov, 2017-08-25 10:50:25

How to organize the search form according to different models?

There are two models and a search form. There are two fields in the form: 1 - search phrase, 2 - select by which you select which model to search for. From the getom form I pass these fields to the view:

class ShortSearchView(AllPagesMixin, ListView):
    '''Результаты быстрого поиска'''

    context_object_name = 'result_list'
    template_name = 'main/search_list.html'
    paginate_by = 20

    def get_queryset(self):
        where = self.request.GET.get('where', '')
        word = self.request.GET.get('word', '')
        if where == '1':
            result = First.objects.filter(name__icontains=word)
        elif where == '2':
            result = Second.objects.filter(name__icontains=word)
        else:
            result = False

        return result

Everything works until pagination appears - object of type 'bool' has no len(), it turns out that when you go to the next page, get_queryset processes the conditions again, but does not get get. Please write what I’m doing wrong, otherwise I’m tormented by vague doubts that in general everything should have been done wrong! Thanks for answers.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Astrohas, 2017-08-25
@AlexandrBirukov

https://stackoverflow.com/a/16609498

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question