I have a few fields I’d like users to be searching across when looking for Restaurants on my site; cuisine_style (m2m), neighborhoods (m2m) and event_space (m2m).
These are presented to the user as dropdown boxes. In addition I’d also like to supply a text field where the user can type in anything and have it search against the restaurants name (CharField) and keywords (CharField).
I can write a view to display the results of ONE of these filters applied, but not all of them. Oh, and each dropdown would obviously have a null/empty default.
Any ideas? Here’s what I got to start with:
from django.db.models import Q
if request.GET.get('q'):
search_terms = request.GET.get('q')
restaurant_list = Restaurant.objects.filter(
Q(name__icontains=search_terms) |
Q(keywords__icontains=search_terms) |
).order_by('-user__date_joined')
but that only covers one field. what about all the dropdowns?
Oh(!) I guess i could compile ALL the GET parameters and split them up and dump them into a list, but not all values are single words.
I am also open to Haystack but I have no idea.
With post and field model form you can prepare a list of Q (named qs in sample) with posteds values:
I assume that your dropdown boxes are ModelMultipleChoiceField.