Django 1.10.1
Search form. The user inserts words separated by spaces. Necessary to find objects with ANY of these words in the title field.
I'm planning to use something like this:
Article.objects.filter(
Q(title__icontains="table") | Q(title__icontains="helm")
)
I can make Q objects easily: q = Q(title__icontains="table").
But the obstacle is how to pass arguments to the filter method.
https://docs.djangoproject.com/en/1.10/topics/db/queries/
The syntax is filter(**kwargs).
With a loop I can prepare a dictionary like this :
q_objects = {"1": Q(title__icontains="table"), "2": Q(title__icontains="glasses")}
But the problem is with that "|".
How to pass it to the filter method is a mystery to me. In other words I fail to build a filter with OR logical operator.
Could you help me?
or
You can do something like this:
In the same line as proposed by @Todor and based on this post, I like this syntax better:
The full code with your example could be: