Reputation: 77
I have an ArrayField
of strings and I want to search if any of the items are containing any of the items within an intended set of strings.
I have defined the field like this:
class Path(models.Model):
path = models.TextField()
files = ArrayField(models.CharField(max_length=300))
server_name = models.CharField(max_length=50)
def __str__(self):
return self.path
And I want to check let's say if the files are containing any name that may contain one of these strings ['foo', 'bar', 'baz']
. That means if the files
is containing the following items:
['example', 'examplebar']
I want it to return true.
I already looked into all the related methods for an ArrayField but none of them seems to work.
I want something that is a combination of contains
and overlap
.
Upvotes: 1
Views: 365
Reputation: 65
It doesn't seem like django supports this directly, so you'll have to do it manually by going through the list and search for the substrings.
If you have a Path
object in the variable path
, and the strings you are searching for in substrings
, you can do this:
for file in path.files:
for s in substrings:
if s in file:
return True
return False
If you want it to be handled purely by the database you'll have to create a new table for the paths and use foreign keys, instead of ArrayField
.
Upvotes: 1