if there is two type of data in table :'ABC-DE' and 'ABCDE' if someone search with space and underscore or hyphen so how to search for it without the regular expression
CodePudding user response:
Q
is the best.
from django.db.models import Q
An example:
posts.filter(Q(title__icontains=query) | Q(tags__name__icontains=query) | Q(body__icontains=query)).order_by('-updated').distinct()
CodePudding user response:
Not so good with response time but you can do like this:
This query will ignore space, hyphen
and you should remove all spec characters from search input as well.
from django.db.models.functions import Lower, Replace
search_input = (search_input.replace(" ","").replace("-","").replace("_","")).lower()
Post.object.annotate(name_lower=Replace(Replace(Lower("name"), Value(" "), Value("")), Value("-"), Value(""))).filter(name_lower__icontains=search_input)