Django filter icons match whole words only

I use filter icons to search for words, but I want it to match whole words. for example, if I were looking for a liver, I would not want it to come back.

my request is as follows

MyModel.objects.filter(title__icontains=search_word) 

I saw the __search filter, but this does not return results with 3 characters or less, and the site I'm building contains many of them that could be searched, for example. "BBC

I don't have access to db, but if someone knows how I can disable this in the code, then I would be happy to switch to using this as an alternative.

+7
django django-queryset
source share
3 answers

Regexp is usually enough: http://docs.djangoproject.com/en/dev/ref/models/querysets/#regex

Note that the regular expression syntax is used in the database backend .

In python (sqlite) regexp will be:

 \b(word)\b 

In Mysql you have:

 mysql> SELECT 'a word a' REGEXP '[[:<:]]word[[:>:]]'; -> 1 mysql> SELECT 'a xword a' REGEXP '[[:<:]]word[[:>:]]'; -> 0 
+6
source share

If you have angularjs and the REST service is implemented with tastypie or DRF, you can filter in whole words like $http.get(uri, {'params': {'display_name__iregex': '[[:<:]]word[[:>:]]'})

of course, display_name must be enabled for filtering in the Tastypie resource meta as filtering = {'display_name': ALL,}

0
source share

Looks like you want an exact case match.

 MyModel.objects.filter(title__iexact=search_word) 

http://docs.djangoproject.com/en/dev/ref/models/querysets/#lookup-iexact

-3
source share

All Articles