Issue
I have a Files
table, which contains various fields, one of which is a filename
. Within each one of the files in this table, there is a specific file that I'd like to retrieve, which is basically a terms and conditions. This filename will always have the words 'terms' and 'conditions' within the filename itself, and it will always be in that order. However, there could be other words surrounding both 'terms' and 'conditions'.
For example, a filename could be 'my new terms and conditions' or 'my even better terms blah blah conditions'.
I'm trying to write a queryset that will retrieve such a file using Regex but I'm not able to do so. So far, I have something like:
file = Files.objects.filter(
Q(filename__iregex=r'(^|\W){}.*({}|$)'.format("term", "condition"))
).first()
but this doesn't work.
Solution
You have few options here.
Filter twice, this will create subquery underneath
file = Files.objects.filter(filename_icontains="term").filter(filename_icontains="condition").first()
You could achieve this via regex, as you've tried. I think simplifying it should work
file = Files.objects.filter(filename__iregex=r"^.*term.*condition.*$").first()
Answered By - Sardorbek Imomaliev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.