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
?
CodePudding user response:
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
CodePudding user response:
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.