Please explain how these 2 lines work:

query = form.cleaned_data['query'] results = Post.objects.annotate(search=SearchVector('title', 'body')).filter(search=query) 

views.py

 def post_search(request): form = SearchForm() query = None results = [] if 'query' in request.GET: form = SearchForm(request.GET) if form.is_valid(): query = form.cleaned_data['query'] results = Post.objects.annotate(search=SearchVector('title', 'body')).filter(search=query) return render(request,'blog/post/search.html',{'form': form, 'query': query, 'results': results}) 

forms.py

 class SearchForm(forms.Form): query = forms.CharField() 

    1 answer 1

    query = form.cleaned_data['query']

    Very simple, we get the search string. For example, a request came in the following form: uri? Tab = 1 & key = value & query = then% 20% 20 entered% 20% 20% search string

    after validating the form, that is, after calling form.is_valid() form.cleaned_data will contain a dictionary: {'query': 'то что ввели в строку поиска'} for more information here .

    annotate indicates how to create a SQL query. SearchVector indicates which fields need full-text search. Through the filter, we pass the "arguments" for the SQL query.

    Read more here and here .

    • Thank you very much, very helpful :) - Morpheu S