How to Query model where name contains any word in python list?
Solution 1
You could use Q
objects to constuct a query like this:
from django.db.models import Q
ob_list = data.objects.filter(reduce(lambda x, y: x | y, [Q(name__contains=word) for word in list]))
Edit:
reduce(lambda x, y: x | y, [Q(name__contains=word) for word in list]))
is a fancy way to write
Q(name__contains=list[0]) | Q(name__contains=list[1]) | ... | Q(name__contains=list[-1])
You could also use an explicit for loop to construct the Q
object.
Solution 2
ob_list = data.objects.filter(name__in=my_list)
And BTW, avoid using the variable name "list" (Or any other python standard keyword), lest you get into some weird bugs later.
Update: (I guess your question was updated too, because when I wrote the answer, I didn't see the part where you wrote you need a contains match and not an exact match)
You can do that using the regex search too, avoiding many Q expressions (which end up using that many where "and" clauses in the SQL, possibly dampening the performance), as follows:
data.objects.filter(name__regex=r'(word1|word2|word3)')
Solution 3
obj_list = [obj for obj in data.objects.all() if any(name in obj.name for name in list)]
Edit: Just re-read your question. Don't know if you can do that with filter
but you can do it with a list comprehension or generator expression.
Solution 4
For anyone comparing Arrays, you could use Django's Overlap filter to achieve this.
From the docs:
Returns objects where the data shares any results with the values passed. Uses the SQL operator &&.
So, you would simply write:
ob_list = data.objects.filter(name__overlap=my_list)
Yugal Jindle
Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid. -- Anonymous Github : YugalJindle Twitter : @YugalJindle Google+ : +YugalJindle LinkedIn : http://www.linkedin.com/in/YugalJindle
Updated on June 08, 2021Comments
-
Yugal Jindle almost 3 years
Aim to Achieve:
I want all objects where name attribute contains any word from the list.
I have:
list = ['word1','word2','word3'] ob_list = data.objects.filter( // What to write here ? ) // or any other way to get the objects where any word in list is contained, in // the na-me attribute of data.
For example:
if name="this is word2":
Then object with such a name should be returned since word2 is in the list.Please help!
-
Yugal Jindle almost 13 yearsName should not be in the list, but any word in the list that is contained in the name.
-
agf almost 13 yearsI already edited my answer -- it now does what you want (in the idiomatic Python way, non-Django specific).
-
Yugal Jindle almost 13 yearscan you explaing.. reduce and lambda...? Your solution is working fine.
-
agf almost 13 yearsI'm not the one who posted this, but
lambda
is just a way of defining a function right there instead of defining it elsewhere, and reduce repeatedly applies an operation repeatedly on a list:reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
does((((1+2)+3)+4)+5)
. The reduction by bitwiseOR
over the list ofQ
s is the same as theany
over thelist
in my answer. -
Ismail Badawi almost 13 years@agf The difference is here the
OR
is performed at the database level instead of fetching all the records and filtering them in python. -
agf almost 13 yearsReally? Django is able to translate the
reduce
into SQL? I think that part is happening in Python inside the ORM. I still wouldn't be suprised if it's faster though. Thein
is happening in the database instead of Python, but theOR
? -
Ismail Badawi almost 13 years@agf Sorry, by the
OR
I meant the actual filtering (i.e. theWHERE
clause hasname LIKE ... OR name LIKE ...
and so on). Thereduce
only constructs theQ
object, which doesn't hit the database. -
Ryder Brooks almost 10 yearsfor my purposes the regex approach was much faster than using Q expressions. And made for a much more readable query. Thank you Lakshman Prasad
-
primoz over 7 yearsNote: If you're using Python > 3, you must import reduce with
from functools import reduce
-
tr0yspradling almost 2 yearsThis only works with PostgreSQL.