[Answered ]-Django/Taggit Searching for posts when all tags and title are contained in the database

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)

Leave a comment