skip to Main Content

In Django data model, Consider that we have a text like this:

I am going to go somewhere.

If I use the Entity.objects.filter(column__contains="go"), it will return going word as well. But I only want the go word to be returned.

Is there any solution other than using Full Text Search?

2

Answers


  1. Chosen as BEST ANSWER

    After checking the Regex document for Django (thanks to Joshlsullivan), This solution worked for me.

    result = Entry.objects.filter(text__regex=rf"(s){searched}(s)" )
    

    In my case, searched is a variable and changes over time (It is not always the word go as in my question). Using {} I was able to add a variable to the regex string. s means white space.


  2. You can loop through the queryset and then return entries with the word in the string. Try something like this:

    for entry in entries:
       if 'go' in entry.column:
          return True
       else:
          return False
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search