61๐
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.
33๐
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)')
- [Django]-Querying django migrations table
- [Django]-Do we need to upload virtual env on github too?
- [Django]-Django: How to set a field to NULL?
2๐
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.
- [Django]-How to make the foreign key field optional in Django model?
- [Django]-Adding extra data to Django Rest Framework results for entire result set
- [Django]-How does the get_or_create function in Django return two values?
1๐
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)
- [Django]-Django url tag multiple parameters
- [Django]-ChoiceField doesn't display an empty label when using a tuple
- [Django]-How do I pass template context information when using HttpResponseRedirect in Django?
0๐
very simple just use "__in" , as the follwing :
my_list= ['word1','word2','word3']
ob_list = model.objects.filter(your-field__in=my_list)
1- change "model" by your model name.
2- change "your-field" by your field name.
i hope this helpful .
- [Django]-How to get the current language in Django?
- [Django]-How to handle per object permission in Django nowadays?
- [Django]-What is the Simplest Possible Payment Gateway to Implement? (using Django)
0๐
Just thought I would add this here. Built this off the "use Q objects" answer with a little tweaking to get search to work for multiple partial word matches across specified fields.
from functools import reduce
from django.contrib.postgres.search import SearchVector
from django.db.models import Q
s = "Input text to match to fields in SearchVector"
words = list(i for i in s.split(" "))
search_data = (
Item.objects.annotate(
search=SearchVector(
"id",
"name",
),
)
.filter(
reduce(
lambda x, y: x & y,
[Q(search__contains=word) for word in words],
)
)
.all()
)
- [Django]-How to set a Django model field's default value to a function call / callable (e.g., a date relative to the time of model object creation)
- [Django]-Django 3.x โ which ASGI server (Uvicorn vs. Daphne)
- [Django]-Django, Turbo Gears, Web2Py, which is better for what?