Как сделать поиск по нескольким полям? Я нашел такое решение, но оно выдает ошибку:
FieldError at /
Related Field got invalid lookup: icontains
from django.db.models import Q
def homepage(request):
books = Book.objects.all()
authors = Author.objects.all()
genre = Genre.objects.all()
search_query = request.GET.get('search', '')
if search_query:
books = Book.objects.filter(Q(title__icontains=search_query) | Q(genre__icontains=search_query))
else:
books = Book.objects.all()
context = {
'books': books,
'authors': authors,
'genre': genre,
}
return render(request, 'main/homepage.html', context=context)