2👍
Try with below code in your views:
from django.db.models import Q
q = request.GET['q'].split() # I am assuming space separator in URL like "random stuff"
query = Q()
for word in q:
query = query | Q(title__icontains=word) | Q(tags__name__icontains=word)
results = Post.objects.filter(query)
0👍
You may want something like this:
def products_search(request):
''' If I am intending to use this in other places also ,I should better put this in Model Manager'''
query_original=request.GET.get('q',None)
search_query = query_original.lower().split()
if len(search_query)>=1:
for word in search_query:
lookups = Q(title__icontains=word) | Q(description__icontains=word) Q(tags__name__icontains=word)
queryset = Product.objects.filter(lookups,is_active=True).distinct()
context = {'object_list':queryset}
return render(request,'products/product_search.html',context)
- [Answered ]-Django remove duplicates in queryset and access their list of foreign key
- [Answered ]-Django url resolvers reverse lookup
- [Answered ]-How can I create simple Django chat based on WebSockets with PostgreSQL database?
- [Answered ]-Save uploaded files in subfolder depending on request
- [Answered ]-Django doesn't use pyodbc as python does
Source:stackexchange.com